1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992 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 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
53 static tree pending_sizes
;
55 /* Nonzero means cannot safely call expand_expr now,
56 so put variable sizes onto `pending_sizes' instead. */
58 int immediate_size_expand
;
63 tree chain
= pending_sizes
;
66 /* Put each SAVE_EXPR into the current function. */
67 for (t
= chain
; t
; t
= TREE_CHAIN (t
))
68 SAVE_EXPR_CONTEXT (TREE_VALUE (t
)) = current_function_decl
;
73 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
74 to serve as the actual size-expression for a type or decl. */
80 size
= save_expr (size
);
82 if (global_bindings_p ())
84 if (TREE_CONSTANT (size
))
85 error ("type size can't be explicitly evaluated");
87 error ("variable-size type declared outside of any function");
92 if (immediate_size_expand
)
93 /* NULL_RTX is not defined; neither is the rtx type. */
94 expand_expr (size
, NULL_PTR
, VOIDmode
, 0);
96 pending_sizes
= tree_cons (NULL_TREE
, size
, pending_sizes
);
101 #ifndef MAX_FIXED_MODE_SIZE
102 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
105 /* Return the machine mode to use for a nonscalar of SIZE bits.
106 The mode must be in class CLASS, and have exactly that many bits.
107 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
111 mode_for_size (size
, class, limit
)
113 enum mode_class
class;
116 register enum machine_mode mode
;
118 if (limit
&& size
> MAX_FIXED_MODE_SIZE
)
121 /* Get the last mode which has this size, in the specified class. */
122 for (mode
= GET_CLASS_NARROWEST_MODE (class); mode
!= VOIDmode
;
123 mode
= GET_MODE_WIDER_MODE (mode
))
124 if (GET_MODE_BITSIZE (mode
) == size
)
130 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
133 round_up (value
, divisor
)
137 return size_binop (MULT_EXPR
,
138 size_binop (CEIL_DIV_EXPR
, value
, size_int (divisor
)),
142 /* Set the size, mode and alignment of a ..._DECL node.
143 TYPE_DECL does need this for C++.
144 Note that LABEL_DECL and CONST_DECL nodes do not need this,
145 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
146 Don't call layout_decl for them.
148 KNOWN_ALIGN is the amount of alignment we can assume this
149 decl has with no special effort. It is relevant only for FIELD_DECLs
150 and depends on the previous fields.
151 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
152 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
153 the record will be aligned to suit. */
156 layout_decl (decl
, known_align
)
158 unsigned known_align
;
160 register tree type
= TREE_TYPE (decl
);
161 register enum tree_code code
= TREE_CODE (decl
);
162 int spec_size
= DECL_FIELD_SIZE (decl
);
164 if (code
== CONST_DECL
)
167 if (code
!= VAR_DECL
&& code
!= PARM_DECL
&& code
!= RESULT_DECL
168 && code
!= FIELD_DECL
&& code
!= TYPE_DECL
)
171 if (type
== error_mark_node
)
173 type
= void_type_node
;
177 /* Usually the size and mode come from the data type without change. */
179 DECL_MODE (decl
) = TYPE_MODE (type
);
180 DECL_SIZE (decl
) = TYPE_SIZE (type
);
181 TREE_UNSIGNED (decl
) = TREE_UNSIGNED (type
);
183 if (code
== FIELD_DECL
&& DECL_BIT_FIELD (decl
))
185 /* This is a bit-field. We don't know how to handle
186 them except for integers and enums, and front end should
187 never generate them otherwise. */
189 if (! (TREE_CODE (type
) == INTEGER_TYPE
190 || TREE_CODE (type
) == ENUMERAL_TYPE
))
193 if (spec_size
== 0 && DECL_NAME (decl
) != 0)
196 /* Size is specified number of bits. */
197 DECL_SIZE (decl
) = size_int (spec_size
);
199 /* Force alignment required for the data type.
200 But if the decl itself wants greater alignment, don't override that.
201 Likewise, if the decl is packed, don't override it. */
202 else if (DECL_ALIGN (decl
) == 0
203 || (! DECL_PACKED (decl
) && TYPE_ALIGN (type
) > DECL_ALIGN (decl
)))
204 DECL_ALIGN (decl
) = TYPE_ALIGN (type
);
206 /* See if we can use an ordinary integer mode for a bit-field. */
207 /* Conditions are: a fixed size that is correct for another mode
208 and occupying a complete byte or bytes on proper boundary. */
209 if (code
== FIELD_DECL
)
211 DECL_BIT_FIELD_TYPE (decl
) = DECL_BIT_FIELD (decl
) ? type
: 0;
212 if (maximum_field_alignment
!= 0)
213 DECL_ALIGN (decl
) = MIN (DECL_ALIGN (decl
), maximum_field_alignment
);
216 if (DECL_BIT_FIELD (decl
)
217 && TYPE_SIZE (type
) != 0
218 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
220 register enum machine_mode xmode
221 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl
)), MODE_INT
, 1);
224 && known_align
% GET_MODE_ALIGNMENT (xmode
) == 0)
226 DECL_ALIGN (decl
) = MAX (GET_MODE_ALIGNMENT (xmode
),
228 DECL_MODE (decl
) = xmode
;
229 DECL_SIZE (decl
) = size_int (GET_MODE_BITSIZE (xmode
));
230 /* This no longer needs to be accessed as a bit field. */
231 DECL_BIT_FIELD (decl
) = 0;
235 /* Evaluate nonconstant size only once, either now or as soon as safe. */
236 if (DECL_SIZE (decl
) != 0 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
237 DECL_SIZE (decl
) = variable_size (DECL_SIZE (decl
));
240 /* Lay out a RECORD_TYPE type (a C struct).
241 This means laying out the fields, determining their positions,
242 and computing the overall size and required alignment of the record.
243 Note that if you set the TYPE_ALIGN before calling this
244 then the struct is aligned to at least that boundary.
246 If the type has basetypes, you must call layout_basetypes
247 before calling this function.
249 The return value is a list of static members of the record.
250 They still need to be laid out. */
257 #ifdef STRUCTURE_SIZE_BOUNDARY
258 unsigned record_align
= MAX (STRUCTURE_SIZE_BOUNDARY
, TYPE_ALIGN (rec
));
260 unsigned record_align
= MAX (BITS_PER_UNIT
, TYPE_ALIGN (rec
));
262 /* These must be laid out *after* the record is. */
263 tree pending_statics
= NULL_TREE
;
264 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
265 where CONST_SIZE is an integer
266 and VAR_SIZE is a tree expression.
267 If VAR_SIZE is null, the size is just CONST_SIZE.
268 Naturally we try to avoid using VAR_SIZE. */
269 register int const_size
= 0;
270 register tree var_size
= 0;
271 /* Once we start using VAR_SIZE, this is the maximum alignment
272 that we know VAR_SIZE has. */
273 register int var_align
= BITS_PER_UNIT
;
276 for (field
= TYPE_FIELDS (rec
); field
; field
= TREE_CHAIN (field
))
278 register int desired_align
;
280 /* If FIELD is static, then treat it like a separate variable,
281 not really like a structure field.
282 If it is a FUNCTION_DECL, it's a method.
283 In both cases, all we do is lay out the decl,
284 and we do it *after* the record is laid out. */
286 if (TREE_STATIC (field
))
288 pending_statics
= tree_cons (NULL_TREE
, field
, pending_statics
);
291 /* Enumerators and enum types which are local to this class need not
292 be laid out. Likewise for initialized constant fields. */
293 if (TREE_CODE (field
) != FIELD_DECL
)
296 /* Lay out the field so we know what alignment it needs.
297 For KNOWN_ALIGN, pass the number of bits from start of record
298 or some divisor of it. */
300 /* For a packed field, use the alignment as specified,
301 disregarding what the type would want. */
302 if (DECL_PACKED (field
))
303 desired_align
= DECL_ALIGN (field
);
304 layout_decl (field
, var_size
? var_align
: const_size
);
305 if (! DECL_PACKED (field
))
306 desired_align
= DECL_ALIGN (field
);
307 /* Some targets (i.e. VMS) limit struct field alignment
308 to a lower boundary than alignment of variables. */
309 #ifdef BIGGEST_FIELD_ALIGNMENT
310 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
313 /* Record must have at least as much alignment as any field.
314 Otherwise, the alignment of the field within the record
317 #ifndef PCC_BITFIELD_TYPE_MATTERS
318 record_align
= MAX (record_align
, desired_align
);
320 if (PCC_BITFIELD_TYPE_MATTERS
&& TREE_TYPE (field
) != error_mark_node
321 && DECL_BIT_FIELD_TYPE (field
)
322 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field
))))
324 /* For these machines, a zero-length field does not
325 affect the alignment of the structure as a whole.
326 It does, however, affect the alignment of the next field
327 within the structure. */
328 if (! integer_zerop (DECL_SIZE (field
)))
329 record_align
= MAX (record_align
, desired_align
);
330 else if (! DECL_PACKED (field
))
331 desired_align
= TYPE_ALIGN (TREE_TYPE (field
));
332 /* A named bit field of declared type `int'
333 forces the entire structure to have `int' alignment. */
334 if (DECL_NAME (field
) != 0)
336 int type_align
= TYPE_ALIGN (TREE_TYPE (field
));
337 if (maximum_field_alignment
!= 0)
338 type_align
= MIN (type_align
, maximum_field_alignment
);
340 record_align
= MAX (record_align
, type_align
);
344 record_align
= MAX (record_align
, desired_align
);
347 /* Does this field automatically have alignment it needs
348 by virtue of the fields that precede it and the record's
351 if (const_size
% desired_align
!= 0
352 || (var_align
% desired_align
!= 0
355 /* No, we need to skip space before this field.
356 Bump the cumulative size to multiple of field alignment. */
359 || var_align
% desired_align
== 0)
361 = CEIL (const_size
, desired_align
) * desired_align
;
365 var_size
= size_binop (PLUS_EXPR
, var_size
,
366 size_int (const_size
));
368 var_size
= round_up (var_size
, desired_align
);
369 var_align
= MIN (var_align
, desired_align
);
373 #ifdef PCC_BITFIELD_TYPE_MATTERS
374 if (PCC_BITFIELD_TYPE_MATTERS
375 && TREE_CODE (field
) == FIELD_DECL
376 && TREE_TYPE (field
) != error_mark_node
377 && DECL_BIT_FIELD_TYPE (field
)
378 && !DECL_PACKED (field
)
379 && !integer_zerop (DECL_SIZE (field
)))
381 int type_align
= TYPE_ALIGN (TREE_TYPE (field
));
382 register tree dsize
= DECL_SIZE (field
);
383 int field_size
= TREE_INT_CST_LOW (dsize
);
385 if (maximum_field_alignment
!= 0)
386 type_align
= MIN (type_align
, maximum_field_alignment
);
388 /* A bit field may not span the unit of alignment of its type.
389 Advance to next boundary if necessary. */
390 /* ??? There is some uncertainty here as to what
391 should be done if type_align is less than the width of the type.
392 That can happen because the width exceeds BIGGEST_ALIGNMENT
393 or because it exceeds maximum_field_alignment. */
394 if (const_size
/ type_align
395 != (const_size
+ field_size
- 1) / type_align
)
396 const_size
= CEIL (const_size
, type_align
) * type_align
;
400 /* No existing machine description uses this parameter.
401 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
402 #ifdef BITFIELD_NBYTES_LIMITED
403 if (BITFIELD_NBYTES_LIMITED
404 && TREE_CODE (field
) == FIELD_DECL
405 && TREE_TYPE (field
) != error_mark_node
406 && DECL_BIT_FIELD_TYPE (field
)
407 && !DECL_PACKED (field
)
408 && !integer_zerop (DECL_SIZE (field
)))
410 int type_align
= TYPE_ALIGN (TREE_TYPE (field
));
411 register tree dsize
= DECL_SIZE (field
);
412 int field_size
= TREE_INT_CST_LOW (dsize
);
414 if (maximum_field_alignment
!= 0)
415 type_align
= MIN (type_align
, maximum_field_alignment
);
417 /* A bit field may not span the unit of alignment of its type.
418 Advance to next boundary if necessary. */
419 if (const_size
/ type_align
420 != (const_size
+ field_size
- 1) / type_align
)
421 const_size
= CEIL (const_size
, type_align
) * type_align
;
425 /* Size so far becomes the position of this field. */
427 if (var_size
&& const_size
)
428 DECL_FIELD_BITPOS (field
)
429 = size_binop (PLUS_EXPR
, var_size
, size_int (const_size
));
431 DECL_FIELD_BITPOS (field
) = var_size
;
433 DECL_FIELD_BITPOS (field
) = size_int (const_size
);
435 /* If this field is an anonymous union,
436 give each union-member the same position as the union has. */
438 if (DECL_NAME (field
) == 0
439 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
441 tree uelt
= TYPE_FIELDS (TREE_TYPE (field
));
442 for (; uelt
; uelt
= TREE_CHAIN (uelt
))
444 DECL_FIELD_CONTEXT (uelt
) = DECL_FIELD_CONTEXT (field
);
445 DECL_FIELD_BITPOS (uelt
) = DECL_FIELD_BITPOS (field
);
449 /* Now add size of this field to the size of the record. */
452 register tree dsize
= DECL_SIZE (field
);
454 /* This can happen when we have an invalid nested struct definition,
455 such as struct j { struct j { int i; } }. The error message is
456 printed in finish_struct. */
459 else if (TREE_CODE (dsize
) == INTEGER_CST
460 && TREE_INT_CST_HIGH (dsize
) == 0
461 && TREE_INT_CST_LOW (dsize
) + const_size
> const_size
)
462 /* Use const_size if there's no overflow. */
463 const_size
+= TREE_INT_CST_LOW (dsize
);
469 var_size
= size_binop (PLUS_EXPR
, var_size
, dsize
);
474 /* Work out the total size and alignment of the record
475 as one expression and store in the record type.
476 Round it up to a multiple of the record's alignment. */
480 TYPE_SIZE (rec
) = size_int (const_size
);
486 = size_binop (PLUS_EXPR
, var_size
, size_int (const_size
));
487 TYPE_SIZE (rec
) = var_size
;
490 /* Determine the desired alignment. */
491 #ifdef ROUND_TYPE_ALIGN
492 TYPE_ALIGN (rec
) = ROUND_TYPE_ALIGN (rec
, TYPE_ALIGN (rec
), record_align
);
494 TYPE_ALIGN (rec
) = MAX (TYPE_ALIGN (rec
), record_align
);
497 #ifdef ROUND_TYPE_SIZE
498 TYPE_SIZE (rec
) = ROUND_TYPE_SIZE (rec
, TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
500 /* Round the size up to be a multiple of the required alignment */
501 TYPE_SIZE (rec
) = round_up (TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
504 return pending_statics
;
507 /* Lay out a UNION_TYPE type.
508 Lay out all the fields, set their positions to zero,
509 and compute the size and alignment of the union (maximum of any field).
510 Note that if you set the TYPE_ALIGN before calling this
511 then the union align is aligned to at least that boundary. */
518 #ifdef STRUCTURE_SIZE_BOUNDARY
519 unsigned union_align
= STRUCTURE_SIZE_BOUNDARY
;
521 unsigned union_align
= BITS_PER_UNIT
;
524 /* The size of the union, based on the fields scanned so far,
525 is max (CONST_SIZE, VAR_SIZE).
526 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
527 register int const_size
= 0;
528 register tree var_size
= 0;
530 for (field
= TYPE_FIELDS (rec
); field
; field
= TREE_CHAIN (field
))
532 /* Enums which are local to this class need not be laid out. */
533 if (TREE_CODE (field
) == CONST_DECL
|| TREE_CODE (field
) == TYPE_DECL
)
536 layout_decl (field
, 0);
537 DECL_FIELD_BITPOS (field
) = size_int (0);
539 /* Union must be at least as aligned as any field requires. */
541 union_align
= MAX (union_align
, DECL_ALIGN (field
));
543 #ifdef PCC_BITFIELD_TYPE_MATTERS
544 /* On the m88000, a bit field of declare type `int'
545 forces the entire union to have `int' alignment. */
546 if (PCC_BITFIELD_TYPE_MATTERS
&& DECL_BIT_FIELD_TYPE (field
))
547 union_align
= MAX (union_align
, TYPE_ALIGN (TREE_TYPE (field
)));
550 /* Set union_size to max (decl_size, union_size).
551 There are more and less general ways to do this.
552 Use only CONST_SIZE unless forced to use VAR_SIZE. */
554 if (TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
555 const_size
= MAX (const_size
, TREE_INT_CST_LOW (DECL_SIZE (field
)));
556 else if (var_size
== 0)
557 var_size
= DECL_SIZE (field
);
559 var_size
= size_binop (MAX_EXPR
, var_size
, DECL_SIZE (field
));
562 /* Determine the ultimate size of the union (in bytes). */
563 if (NULL
== var_size
)
564 TYPE_SIZE (rec
) = size_int (CEIL (const_size
, BITS_PER_UNIT
)
566 else if (const_size
== 0)
567 TYPE_SIZE (rec
) = var_size
;
569 TYPE_SIZE (rec
) = size_binop (MAX_EXPR
, var_size
,
570 round_up (size_int (const_size
),
573 /* Determine the desired alignment. */
574 #ifdef ROUND_TYPE_ALIGN
575 TYPE_ALIGN (rec
) = ROUND_TYPE_ALIGN (rec
, TYPE_ALIGN (rec
), union_align
);
577 TYPE_ALIGN (rec
) = MAX (TYPE_ALIGN (rec
), union_align
);
580 #ifdef ROUND_TYPE_SIZE
581 TYPE_SIZE (rec
) = ROUND_TYPE_SIZE (rec
, TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
583 /* Round the size up to be a multiple of the required alignment */
584 TYPE_SIZE (rec
) = round_up (TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
588 /* Calculate the mode, size, and alignment for TYPE.
589 For an array type, calculate the element separation as well.
590 Record TYPE on the chain of permanent or temporary types
591 so that dbxout will find out about it.
593 TYPE_SIZE of a type is nonzero if the type has been laid out already.
594 layout_type does nothing on such a type.
596 If the type is incomplete, its TYPE_SIZE remains zero. */
603 tree pending_statics
;
608 /* Do nothing if type has been laid out before. */
609 if (TYPE_SIZE (type
))
612 /* Make sure all nodes we allocate are not momentary;
613 they must last past the current statement. */
614 old
= suspend_momentary ();
616 /* If we are processing a permanent type, make nodes permanent.
617 If processing a temporary type, make it saveable, since the
618 type node itself is. This is important if the function is inline,
619 since its decls will get copied later. */
620 push_obstacks_nochange ();
621 if (allocation_temporary_p ())
623 if (TREE_PERMANENT (type
))
624 end_temporary_allocation ();
626 saveable_allocation ();
629 switch (TREE_CODE (type
))
632 /* This kind of type is the responsibility
633 of the languge-specific code. */
638 if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type
)) >= 0)
639 TREE_UNSIGNED (type
) = 1;
641 /* We pass 0 for the last arg of mode_for_size because otherwise
642 on the Apollo using long long causes a crash.
643 It seems better to use integer modes than to try to support
644 integer types with BLKmode. */
645 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_INT
, 0);
646 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
650 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_FLOAT
, 0);
651 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
655 TREE_UNSIGNED (type
) = TREE_UNSIGNED (TREE_TYPE (type
));
657 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type
)),
658 (TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
659 ? MODE_COMPLEX_INT
: MODE_COMPLEX_FLOAT
),
661 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
665 TYPE_SIZE (type
) = size_zero_node
;
666 TYPE_ALIGN (type
) = 1;
667 TYPE_MODE (type
) = VOIDmode
;
671 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (Pmode
));
672 TYPE_MODE (type
) = Pmode
;
677 TYPE_MODE (type
) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode
),
679 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
684 TYPE_MODE (type
) = Pmode
;
685 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
686 TREE_UNSIGNED (type
) = 1;
687 TYPE_PRECISION (type
) = GET_MODE_BITSIZE (TYPE_MODE (type
));
692 register tree index
= TYPE_DOMAIN (type
);
693 register tree element
= TREE_TYPE (type
);
695 build_pointer_type (element
);
697 /* We need to know both bounds in order to compute the size. */
698 if (index
&& TYPE_MAX_VALUE (index
) && TYPE_MIN_VALUE (index
)
699 && TYPE_SIZE (element
))
702 = size_binop (PLUS_EXPR
, size_one_node
,
703 size_binop (MINUS_EXPR
, TYPE_MAX_VALUE (index
),
704 TYPE_MIN_VALUE (index
)));
706 TYPE_SIZE (type
) = size_binop (MULT_EXPR
, length
,
707 TYPE_SIZE (element
));
710 /* Now round the alignment and size,
711 using machine-dependent criteria if any. */
713 #ifdef ROUND_TYPE_ALIGN
715 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (element
), BITS_PER_UNIT
);
717 TYPE_ALIGN (type
) = MAX (TYPE_ALIGN (element
), BITS_PER_UNIT
);
720 #ifdef ROUND_TYPE_SIZE
721 if (TYPE_SIZE (type
) != 0)
723 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
726 TYPE_MODE (type
) = BLKmode
;
727 if (TYPE_SIZE (type
) != 0
728 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
729 /* BLKmode elements force BLKmode aggregate;
730 else extract/store fields may lose. */
731 && (TYPE_MODE (TREE_TYPE (type
)) != BLKmode
732 || TYPE_NO_FORCE_BLK (TREE_TYPE (type
))))
735 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
738 if (STRICT_ALIGNMENT
&& TYPE_ALIGN (type
) < BIGGEST_ALIGNMENT
739 && TYPE_ALIGN (type
) < TREE_INT_CST_LOW (TYPE_SIZE (type
))
740 && TYPE_MODE (type
) != BLKmode
)
742 TYPE_NO_FORCE_BLK (type
) = 1;
743 TYPE_MODE (type
) = BLKmode
;
750 pending_statics
= layout_record (type
);
751 TYPE_MODE (type
) = BLKmode
;
752 if (TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
755 /* A record which has any BLKmode members must itself be BLKmode;
756 it can't go in a register.
757 Unless the member is BLKmode only because it isn't aligned. */
758 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
762 if (TREE_CODE (field
) != FIELD_DECL
)
765 if (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
766 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
769 if (TREE_CODE (DECL_FIELD_BITPOS (field
)) != INTEGER_CST
)
772 bitpos
= TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field
));
774 /* Must be BLKmode if any field crosses a word boundary,
775 since extract_bit_field can't handle that in registers. */
776 if (bitpos
/ BITS_PER_WORD
777 != ((TREE_INT_CST_LOW (DECL_SIZE (field
)) + bitpos
- 1)
779 /* But there is no problem if the field is entire words. */
780 && TREE_INT_CST_LOW (DECL_SIZE (field
)) % BITS_PER_WORD
== 0)
785 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
788 /* If structure's known alignment is less than
789 what the scalar mode would need, and it matters,
790 then stick with BLKmode. */
792 && ! (TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
793 || (TYPE_ALIGN (type
)
794 >= TREE_INT_CST_LOW (TYPE_SIZE (type
)))))
796 if (TYPE_MODE (type
) != BLKmode
)
797 /* If this is the only reason this type is BLKmode,
798 then don't force containing types to be BLKmode. */
799 TYPE_NO_FORCE_BLK (type
) = 1;
800 TYPE_MODE (type
) = BLKmode
;
806 /* Lay out any static members. This is done now
807 because their type may use the record's type. */
808 while (pending_statics
)
810 layout_decl (TREE_VALUE (pending_statics
), 0);
811 pending_statics
= TREE_CHAIN (pending_statics
);
817 TYPE_MODE (type
) = BLKmode
;
818 if (TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
819 /* If structure's known alignment is less than
820 what the scalar mode would need, and it matters,
821 then stick with BLKmode. */
822 && (! STRICT_ALIGNMENT
823 || TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
824 || TYPE_ALIGN (type
) >= TREE_INT_CST_LOW (TYPE_SIZE (type
))))
827 /* A union which has any BLKmode members must itself be BLKmode;
828 it can't go in a register.
829 Unless the member is BLKmode only because it isn't aligned. */
830 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
832 if (TREE_CODE (field
) != FIELD_DECL
)
835 if (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
836 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
841 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
849 case BOOLEAN_TYPE
: /* store one byte/boolean for now. */
850 TYPE_MODE (type
) = QImode
;
851 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
852 TYPE_PRECISION (type
) = 1;
853 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
857 TYPE_MODE (type
) = QImode
;
858 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
859 TYPE_PRECISION (type
) = GET_MODE_BITSIZE (TYPE_MODE (type
));
860 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
864 /* The size may vary in different languages, so the language front end
865 should fill in the size. */
866 TYPE_ALIGN (type
) = BIGGEST_ALIGNMENT
;
867 TYPE_MODE (type
) = BLKmode
;
874 /* Normally, use the alignment corresponding to the mode chosen.
875 However, where strict alignment is not required, avoid
876 over-aligning structures, since most compilers do not do this
879 if (TYPE_MODE (type
) != BLKmode
&& TYPE_MODE (type
) != VOIDmode
881 || (TREE_CODE (type
) != RECORD_TYPE
&& TREE_CODE (type
) != UNION_TYPE
882 && TREE_CODE (type
) != ARRAY_TYPE
)))
883 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
885 /* Evaluate nonconstant size only once, either now or as soon as safe. */
886 if (TYPE_SIZE (type
) != 0 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
887 TYPE_SIZE (type
) = variable_size (TYPE_SIZE (type
));
889 /* Also layout any other variants of the type. */
890 if (TYPE_NEXT_VARIANT (type
)
891 || type
!= TYPE_MAIN_VARIANT (type
))
894 /* Record layout info of this variant. */
895 tree size
= TYPE_SIZE (type
);
896 int align
= TYPE_ALIGN (type
);
897 enum machine_mode mode
= TYPE_MODE (type
);
899 /* Copy it into all variants. */
900 for (variant
= TYPE_MAIN_VARIANT (type
);
902 variant
= TYPE_NEXT_VARIANT (variant
))
904 TYPE_SIZE (variant
) = size
;
905 TYPE_ALIGN (variant
) = align
;
906 TYPE_MODE (variant
) = mode
;
911 resume_momentary (old
);
914 /* Create and return a type for signed integers of PRECISION bits. */
917 make_signed_type (precision
)
920 register tree type
= make_node (INTEGER_TYPE
);
922 TYPE_PRECISION (type
) = precision
;
924 /* Create the extreme values based on the number of bits. */
926 TYPE_MIN_VALUE (type
)
927 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
928 ? 0 : (HOST_WIDE_INT
) (-1) << (precision
- 1)),
929 (((HOST_WIDE_INT
) (-1)
930 << (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
931 ? precision
- HOST_BITS_PER_WIDE_INT
- 1
933 TYPE_MAX_VALUE (type
)
934 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
935 ? -1 : ((HOST_WIDE_INT
) 1 << (precision
- 1)) - 1),
936 (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
937 ? (((HOST_WIDE_INT
) 1
938 << (precision
- HOST_BITS_PER_WIDE_INT
- 1))) - 1
941 /* Give this type's extreme values this type as their type. */
943 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
944 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
946 /* The first type made with this or `make_unsigned_type'
947 is the type for size values. */
954 /* Lay out the type: set its alignment, size, etc. */
961 /* Create and return a type for unsigned integers of PRECISION bits. */
964 make_unsigned_type (precision
)
967 register tree type
= make_node (INTEGER_TYPE
);
969 TYPE_PRECISION (type
) = precision
;
971 /* The first type made with this or `make_signed_type'
972 is the type for size values. */
979 fixup_unsigned_type (type
);
983 /* Set the extreme values of TYPE based on its precision in bits,
984 then lay it out. Used when make_signed_type won't do
985 because the tree code is not INTEGER_TYPE.
986 E.g. for Pascal, when the -fsigned-char option is given. */
989 fixup_signed_type (type
)
992 register int precision
= TYPE_PRECISION (type
);
994 TYPE_MIN_VALUE (type
)
995 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
996 ? 0 : (HOST_WIDE_INT
) (-1) << (precision
- 1)),
997 (((HOST_WIDE_INT
) (-1)
998 << (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
999 ? precision
- HOST_BITS_PER_WIDE_INT
- 1
1001 TYPE_MAX_VALUE (type
)
1002 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
1003 ? -1 : ((HOST_WIDE_INT
) 1 << (precision
- 1)) - 1),
1004 (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
1005 ? (((HOST_WIDE_INT
) 1
1006 << (precision
- HOST_BITS_PER_WIDE_INT
- 1))) - 1
1009 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1010 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1012 /* Lay out the type: set its alignment, size, etc. */
1017 /* Set the extreme values of TYPE based on its precision in bits,
1018 then lay it out. This is used both in `make_unsigned_type'
1019 and for enumeral types. */
1022 fixup_unsigned_type (type
)
1025 register int precision
= TYPE_PRECISION (type
);
1027 TYPE_MIN_VALUE (type
) = build_int_2 (0, 0);
1028 TYPE_MAX_VALUE (type
)
1029 = build_int_2 (precision
- HOST_BITS_PER_WIDE_INT
>= 0
1030 ? -1 : ((HOST_WIDE_INT
) 1 << precision
) - 1,
1031 precision
- HOST_BITS_PER_WIDE_INT
> 0
1032 ? ((unsigned HOST_WIDE_INT
) ~0
1033 >> (HOST_BITS_PER_WIDE_INT
1034 - (precision
- HOST_BITS_PER_WIDE_INT
)))
1036 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1037 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1039 /* Lay out the type: set its alignment, size, etc. */
1044 /* Find the best machine mode to use when referencing a bit field of length
1045 BITSIZE bits starting at BITPOS.
1047 The underlying object is known to be aligned to a boundary of ALIGN bits.
1048 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1049 larger than LARGEST_MODE (usually SImode).
1051 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1052 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1053 mode meeting these conditions.
1055 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1056 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1057 all the conditions. */
1060 get_best_mode (bitsize
, bitpos
, align
, largest_mode
, volatilep
)
1061 int bitsize
, bitpos
;
1063 enum machine_mode largest_mode
;
1066 enum machine_mode mode
;
1069 /* Find the narrowest integer mode that contains the bit field. */
1070 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1071 mode
= GET_MODE_WIDER_MODE (mode
))
1073 unit
= GET_MODE_BITSIZE (mode
);
1074 if (bitpos
/ unit
== (bitpos
+ bitsize
- 1) / unit
)
1078 if (mode
== MAX_MACHINE_MODE
1079 /* It is tempting to omit the following line
1080 if STRICT_ALIGNMENT is true.
1081 But that is incorrect, since if the bitfield uses part of 3 bytes
1082 and we use a 4-byte mode, we could get a spurious segv
1083 if the extra 4th byte is past the end of memory.
1084 (Though at least one Unix compiler ignores this problem:
1085 that on the Sequent 386 machine. */
1086 || MIN (unit
, BIGGEST_ALIGNMENT
) > align
1087 || (largest_mode
!= VOIDmode
&& unit
> GET_MODE_BITSIZE (largest_mode
)))
1090 if (SLOW_BYTE_ACCESS
&& ! volatilep
)
1092 enum machine_mode wide_mode
= VOIDmode
, tmode
;
1094 for (tmode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); tmode
!= VOIDmode
;
1095 tmode
= GET_MODE_WIDER_MODE (tmode
))
1097 unit
= GET_MODE_BITSIZE (tmode
);
1098 if (bitpos
/ unit
== (bitpos
+ bitsize
- 1) / unit
1099 && unit
<= BITS_PER_WORD
1100 && unit
<= MIN (align
, BIGGEST_ALIGNMENT
)
1101 && (largest_mode
== VOIDmode
1102 || unit
<= GET_MODE_BITSIZE (largest_mode
)))
1106 if (wide_mode
!= VOIDmode
)
1113 /* Save all variables describing the current status into the structure *P.
1114 This is used before starting a nested function. */
1117 save_storage_status (p
)
1120 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1121 p
->pending_sizes
= pending_sizes
;
1122 p
->immediate_size_expand
= immediate_size_expand
;
1126 /* Restore all variables describing the current status from the structure *P.
1127 This is used after a nested function. */
1130 restore_storage_status (p
)
1134 pending_sizes
= p
->pending_sizes
;
1135 immediate_size_expand
= p
->immediate_size_expand
;