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 #define GET_MODE_ALIGNMENT(MODE) \
44 MIN (BIGGEST_ALIGNMENT, \
45 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
47 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
49 static tree pending_sizes
;
51 /* Nonzero means cannot safely call expand_expr now,
52 so put variable sizes onto `pending_sizes' instead. */
54 int immediate_size_expand
;
59 tree chain
= pending_sizes
;
62 /* Put each SAVE_EXPR into the current function. */
63 for (t
= chain
; t
; t
= TREE_CHAIN (t
))
64 SAVE_EXPR_CONTEXT (TREE_VALUE (t
)) = current_function_decl
;
69 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
70 to serve as the actual size-expression for a type or decl. */
76 size
= save_expr (size
);
78 if (global_bindings_p ())
80 error ("variable-size type declared outside of any function");
84 if (immediate_size_expand
)
85 expand_expr (size
, 0, VOIDmode
, 0);
87 pending_sizes
= tree_cons (0, size
, pending_sizes
);
92 #ifndef MAX_FIXED_MODE_SIZE
93 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
96 /* Return the machine mode to use for a nonscalar of SIZE bits.
97 The mode must be in class CLASS, and have exactly that many bits.
98 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
102 mode_for_size (size
, class, limit
)
104 enum mode_class
class;
107 register enum machine_mode mode
;
109 if (limit
&& size
> MAX_FIXED_MODE_SIZE
)
112 /* Get the last mode which has this size, in the specified class. */
113 for (mode
= GET_CLASS_NARROWEST_MODE (class); mode
!= VOIDmode
;
114 mode
= GET_MODE_WIDER_MODE (mode
))
115 if (GET_MODE_BITSIZE (mode
) == size
)
121 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
124 round_up (value
, divisor
)
128 return size_binop (MULT_EXPR
,
129 size_binop (CEIL_DIV_EXPR
, value
, size_int (divisor
)),
133 /* Set the size, mode and alignment of a ..._DECL node.
134 TYPE_DECL does need this for C++.
135 Note that LABEL_DECL and CONST_DECL nodes do not need this,
136 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
137 Don't call layout_decl for them.
139 KNOWN_ALIGN is the amount of alignment we can assume this
140 decl has with no special effort. It is relevant only for FIELD_DECLs
141 and depends on the previous fields.
142 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
143 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
144 the record will be aligned to suit. */
147 layout_decl (decl
, known_align
)
149 unsigned known_align
;
151 register tree type
= TREE_TYPE (decl
);
152 register enum tree_code code
= TREE_CODE (decl
);
153 int spec_size
= DECL_FIELD_SIZE (decl
);
155 if (code
== CONST_DECL
)
158 if (code
!= VAR_DECL
&& code
!= PARM_DECL
&& code
!= RESULT_DECL
159 && code
!= FIELD_DECL
&& code
!= TYPE_DECL
)
162 if (type
== error_mark_node
)
164 type
= void_type_node
;
168 /* Usually the size and mode come from the data type without change. */
170 DECL_MODE (decl
) = TYPE_MODE (type
);
171 DECL_SIZE (decl
) = TYPE_SIZE (type
);
172 TREE_UNSIGNED (decl
) = TREE_UNSIGNED (type
);
174 if (code
== FIELD_DECL
&& DECL_BIT_FIELD (decl
))
176 /* This is a bit-field. We don't know how to handle
177 them except for integers and enums, and front end should
178 never generate them otherwise. */
180 if (! (TREE_CODE (type
) == INTEGER_TYPE
181 || TREE_CODE (type
) == ENUMERAL_TYPE
))
184 if (spec_size
== 0 && DECL_NAME (decl
) != 0)
187 /* Size is specified number of bits. */
188 DECL_SIZE (decl
) = size_int (spec_size
);
190 /* Force alignment required for the data type.
191 But if the decl itself wants greater alignment, don't override that.
192 Likewise, if the decl is packed, don't override it. */
193 else if (DECL_ALIGN (decl
) == 0
194 || (! DECL_PACKED (decl
) && TYPE_ALIGN (type
) > DECL_ALIGN (decl
)))
195 DECL_ALIGN (decl
) = TYPE_ALIGN (type
);
197 /* See if we can use an ordinary integer mode for a bit-field. */
198 /* Conditions are: a fixed size that is correct for another mode
199 and occupying a complete byte or bytes on proper boundary. */
200 if (code
== FIELD_DECL
)
201 DECL_BIT_FIELD_TYPE (decl
) = DECL_BIT_FIELD (decl
) ? type
: 0;
202 if (DECL_BIT_FIELD (decl
)
203 && TYPE_SIZE (type
) != 0
204 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
206 register enum machine_mode xmode
207 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl
)), MODE_INT
, 1);
210 && known_align
% GET_MODE_ALIGNMENT (xmode
) == 0)
212 DECL_ALIGN (decl
) = MAX (GET_MODE_ALIGNMENT (xmode
),
214 DECL_MODE (decl
) = xmode
;
215 DECL_SIZE (decl
) = size_int (GET_MODE_BITSIZE (xmode
));
216 /* This no longer needs to be accessed as a bit field. */
217 DECL_BIT_FIELD (decl
) = 0;
221 /* Evaluate nonconstant size only once, either now or as soon as safe. */
222 if (DECL_SIZE (decl
) != 0 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
223 DECL_SIZE (decl
) = variable_size (DECL_SIZE (decl
));
226 /* Lay out a RECORD_TYPE type (a C struct).
227 This means laying out the fields, determining their positions,
228 and computing the overall size and required alignment of the record.
229 Note that if you set the TYPE_ALIGN before calling this
230 then the struct is aligned to at least that boundary.
232 If the type has basetypes, you must call layout_basetypes
233 before calling this function.
235 The return value is a list of static members of the record.
236 They still need to be laid out. */
243 #ifdef STRUCTURE_SIZE_BOUNDARY
244 unsigned record_align
= MAX (STRUCTURE_SIZE_BOUNDARY
, TYPE_ALIGN (rec
));
246 unsigned record_align
= MAX (BITS_PER_UNIT
, TYPE_ALIGN (rec
));
248 /* These must be laid out *after* the record is. */
249 tree pending_statics
= NULL_TREE
;
250 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
251 where CONST_SIZE is an integer
252 and VAR_SIZE is a tree expression.
253 If VAR_SIZE is null, the size is just CONST_SIZE.
254 Naturally we try to avoid using VAR_SIZE. */
255 register int const_size
= 0;
256 register tree var_size
= 0;
257 /* Once we start using VAR_SIZE, this is the maximum alignment
258 that we know VAR_SIZE has. */
259 register int var_align
= BITS_PER_UNIT
;
262 for (field
= TYPE_FIELDS (rec
); field
; field
= TREE_CHAIN (field
))
264 register int desired_align
;
266 /* If FIELD is static, then treat it like a separate variable,
267 not really like a structure field.
268 If it is a FUNCTION_DECL, it's a method.
269 In both cases, all we do is lay out the decl,
270 and we do it *after* the record is laid out. */
272 if (TREE_STATIC (field
))
274 pending_statics
= tree_cons (NULL
, field
, pending_statics
);
277 /* Enumerators and enum types which are local to this class need not
278 be laid out. Likewise for initialized constant fields. */
279 if (TREE_CODE (field
) != FIELD_DECL
)
282 /* Lay out the field so we know what alignment it needs.
283 For KNOWN_ALIGN, pass the number of bits from start of record
284 or some divisor of it. */
286 /* For a packed field, use the alignment as specified,
287 disregarding what the type would want. */
288 if (DECL_PACKED (field
))
289 desired_align
= DECL_ALIGN (field
);
290 layout_decl (field
, var_size
? var_align
: const_size
);
291 if (! DECL_PACKED (field
))
292 desired_align
= DECL_ALIGN (field
);
293 /* Some targets (i.e. VMS) limit struct field alignment
294 to a lower boundary than alignment of variables. */
295 #ifdef BIGGEST_FIELD_ALIGNMENT
296 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
299 /* Record must have at least as much alignment as any field.
300 Otherwise, the alignment of the field within the record
303 #ifndef PCC_BITFIELD_TYPE_MATTERS
304 record_align
= MAX (record_align
, desired_align
);
306 if (PCC_BITFIELD_TYPE_MATTERS
&& TREE_TYPE (field
) != error_mark_node
307 && DECL_BIT_FIELD_TYPE (field
)
308 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field
))))
310 /* For these machines, a zero-length field does not
311 affect the alignment of the structure as a whole.
312 It does, however, affect the alignment of the next field
313 within the structure. */
314 if (! integer_zerop (DECL_SIZE (field
)))
315 record_align
= MAX (record_align
, desired_align
);
316 else if (! DECL_PACKED (field
))
317 desired_align
= TYPE_ALIGN (TREE_TYPE (field
));
318 /* A named bit field of declared type `int'
319 forces the entire structure to have `int' alignment. */
320 if (DECL_NAME (field
) != 0)
321 record_align
= MAX (record_align
, TYPE_ALIGN (TREE_TYPE (field
)));
324 record_align
= MAX (record_align
, desired_align
);
327 /* Does this field automatically have alignment it needs
328 by virtue of the fields that precede it and the record's
331 if (const_size
% desired_align
!= 0
332 || (var_align
% desired_align
!= 0
335 /* No, we need to skip space before this field.
336 Bump the cumulative size to multiple of field alignment. */
339 || var_align
% desired_align
== 0)
341 = CEIL (const_size
, desired_align
) * desired_align
;
345 var_size
= size_binop (PLUS_EXPR
, var_size
,
346 size_int (const_size
));
348 var_size
= round_up (var_size
, desired_align
);
349 var_align
= MIN (var_align
, desired_align
);
353 #ifdef PCC_BITFIELD_TYPE_MATTERS
354 if (PCC_BITFIELD_TYPE_MATTERS
355 && TREE_CODE (field
) == FIELD_DECL
356 && TREE_TYPE (field
) != error_mark_node
357 && DECL_BIT_FIELD_TYPE (field
)
358 && !DECL_PACKED (field
)
359 && !integer_zerop (DECL_SIZE (field
)))
361 int type_align
= TYPE_ALIGN (TREE_TYPE (field
));
362 register tree dsize
= DECL_SIZE (field
);
363 int field_size
= TREE_INT_CST_LOW (dsize
);
365 /* A bit field may not span the unit of alignment of its type.
366 Advance to next boundary if necessary. */
367 if (const_size
/ type_align
368 != (const_size
+ field_size
- 1) / type_align
)
369 const_size
= CEIL (const_size
, type_align
) * type_align
;
373 /* No existing machine description uses this parameter.
374 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
375 #ifdef BITFIELD_NBYTES_LIMITED
376 if (BITFIELD_NBYTES_LIMITED
377 && TREE_CODE (field
) == FIELD_DECL
378 && TREE_TYPE (field
) != error_mark_node
379 && DECL_BIT_FIELD_TYPE (field
)
380 && !DECL_PACKED (field
)
381 && !integer_zerop (DECL_SIZE (field
)))
383 int type_align
= TYPE_ALIGN (TREE_TYPE (field
));
384 register tree dsize
= DECL_SIZE (field
);
385 int field_size
= TREE_INT_CST_LOW (dsize
);
387 /* A bit field may not span the unit of alignment of its type.
388 Advance to next boundary if necessary. */
389 if (const_size
/ type_align
390 != (const_size
+ field_size
- 1) / type_align
)
391 const_size
= CEIL (const_size
, type_align
) * type_align
;
395 /* Size so far becomes the position of this field. */
397 if (var_size
&& const_size
)
398 DECL_FIELD_BITPOS (field
)
399 = size_binop (PLUS_EXPR
, var_size
, size_int (const_size
));
401 DECL_FIELD_BITPOS (field
) = var_size
;
403 DECL_FIELD_BITPOS (field
) = size_int (const_size
);
405 /* If this field is an anonymous union,
406 give each union-member the same position as the union has. */
408 if (DECL_NAME (field
) == 0
409 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
411 tree uelt
= TYPE_FIELDS (TREE_TYPE (field
));
412 for (; uelt
; uelt
= TREE_CHAIN (uelt
))
414 DECL_FIELD_CONTEXT (uelt
) = DECL_FIELD_CONTEXT (field
);
415 DECL_FIELD_BITPOS (uelt
) = DECL_FIELD_BITPOS (field
);
419 /* Now add size of this field to the size of the record. */
422 register tree dsize
= DECL_SIZE (field
);
424 if (TREE_CODE (dsize
) == INTEGER_CST
)
425 const_size
+= TREE_INT_CST_LOW (dsize
);
431 var_size
= size_binop (PLUS_EXPR
, var_size
, dsize
);
436 /* Work out the total size and alignment of the record
437 as one expression and store in the record type.
438 Round it up to a multiple of the record's alignment. */
442 TYPE_SIZE (rec
) = size_int (const_size
);
448 = size_binop (PLUS_EXPR
, var_size
, size_int (const_size
));
449 TYPE_SIZE (rec
) = var_size
;
452 /* Determine the desired alignment. */
453 #ifdef ROUND_TYPE_ALIGN
454 TYPE_ALIGN (rec
) = ROUND_TYPE_ALIGN (rec
, TYPE_ALIGN (rec
), record_align
);
456 TYPE_ALIGN (rec
) = MAX (TYPE_ALIGN (rec
), record_align
);
459 #ifdef ROUND_TYPE_SIZE
460 TYPE_SIZE (rec
) = ROUND_TYPE_SIZE (rec
, TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
462 /* Round the size up to be a multiple of the required alignment */
463 TYPE_SIZE (rec
) = round_up (TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
466 return pending_statics
;
469 /* Lay out a UNION_TYPE type.
470 Lay out all the fields, set their positions to zero,
471 and compute the size and alignment of the union (maximum of any field).
472 Note that if you set the TYPE_ALIGN before calling this
473 then the union align is aligned to at least that boundary. */
480 #ifdef STRUCTURE_SIZE_BOUNDARY
481 unsigned union_align
= STRUCTURE_SIZE_BOUNDARY
;
483 unsigned union_align
= BITS_PER_UNIT
;
486 /* The size of the union, based on the fields scanned so far,
487 is max (CONST_SIZE, VAR_SIZE).
488 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
489 register int const_size
= 0;
490 register tree var_size
= 0;
492 for (field
= TYPE_FIELDS (rec
); field
; field
= TREE_CHAIN (field
))
494 /* Enums which are local to this class need not be laid out. */
495 if (TREE_CODE (field
) == CONST_DECL
|| TREE_CODE (field
) == TYPE_DECL
)
498 layout_decl (field
, 0);
499 DECL_FIELD_BITPOS (field
) = size_int (0);
501 /* Union must be at least as aligned as any field requires. */
503 union_align
= MAX (union_align
, DECL_ALIGN (field
));
505 #ifdef PCC_BITFIELD_TYPE_MATTERS
506 /* On the m88000, a bit field of declare type `int'
507 forces the entire union to have `int' alignment. */
508 if (PCC_BITFIELD_TYPE_MATTERS
&& DECL_BIT_FIELD_TYPE (field
))
509 union_align
= MAX (union_align
, TYPE_ALIGN (TREE_TYPE (field
)));
512 /* Set union_size to max (decl_size, union_size).
513 There are more and less general ways to do this.
514 Use only CONST_SIZE unless forced to use VAR_SIZE. */
516 if (TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
517 const_size
= MAX (const_size
, TREE_INT_CST_LOW (DECL_SIZE (field
)));
518 else if (var_size
== 0)
519 var_size
= DECL_SIZE (field
);
521 var_size
= size_binop (MAX_EXPR
, var_size
, DECL_SIZE (field
));
524 /* Determine the ultimate size of the union (in bytes). */
525 if (NULL
== var_size
)
526 TYPE_SIZE (rec
) = size_int (CEIL (const_size
, BITS_PER_UNIT
)
528 else if (const_size
== 0)
529 TYPE_SIZE (rec
) = var_size
;
531 TYPE_SIZE (rec
) = size_binop (MAX_EXPR
, var_size
,
532 round_up (size_int (const_size
),
535 /* Determine the desired alignment. */
536 #ifdef ROUND_TYPE_ALIGN
537 TYPE_ALIGN (rec
) = ROUND_TYPE_ALIGN (rec
, TYPE_ALIGN (rec
), union_align
);
539 TYPE_ALIGN (rec
) = MAX (TYPE_ALIGN (rec
), union_align
);
542 #ifdef ROUND_TYPE_SIZE
543 TYPE_SIZE (rec
) = ROUND_TYPE_SIZE (rec
, TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
545 /* Round the size up to be a multiple of the required alignment */
546 TYPE_SIZE (rec
) = round_up (TYPE_SIZE (rec
), TYPE_ALIGN (rec
));
550 /* Calculate the mode, size, and alignment for TYPE.
551 For an array type, calculate the element separation as well.
552 Record TYPE on the chain of permanent or temporary types
553 so that dbxout will find out about it.
555 TYPE_SIZE of a type is nonzero if the type has been laid out already.
556 layout_type does nothing on such a type.
558 If the type is incomplete, its TYPE_SIZE remains zero. */
565 tree pending_statics
;
570 /* Do nothing if type has been laid out before. */
571 if (TYPE_SIZE (type
))
574 /* Make sure all nodes we allocate are not momentary;
575 they must last past the current statement. */
576 old
= suspend_momentary ();
578 /* If we are processing a permanent type, make nodes permanent.
579 If processing a temporary type, make it saveable, since the
580 type node itself is. This is important if the function is inline,
581 since its decls will get copied later. */
582 push_obstacks_nochange ();
583 if (allocation_temporary_p ())
585 if (TREE_PERMANENT (type
))
586 end_temporary_allocation ();
588 saveable_allocation ();
591 switch (TREE_CODE (type
))
594 /* This kind of type is the responsibility
595 of the languge-specific code. */
600 if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type
)) >= 0)
601 TREE_UNSIGNED (type
) = 1;
603 /* We pass 0 for the last arg of mode_for_size because otherwise
604 on the Apollo using long long causes a crash.
605 It seems better to use integer modes than to try to support
606 integer types with BLKmode. */
607 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_INT
, 0);
608 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
612 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_FLOAT
, 0);
613 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
617 TREE_UNSIGNED (type
) = TREE_UNSIGNED (TREE_TYPE (type
));
619 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type
)),
620 (TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
621 ? MODE_COMPLEX_INT
: MODE_COMPLEX_FLOAT
),
623 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
627 TYPE_SIZE (type
) = size_zero_node
;
628 TYPE_ALIGN (type
) = 1;
629 TYPE_MODE (type
) = VOIDmode
;
633 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (Pmode
));
634 TYPE_MODE (type
) = Pmode
;
639 TYPE_MODE (type
) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode
),
641 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
646 TYPE_MODE (type
) = Pmode
;
647 TYPE_SIZE (type
) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
648 TREE_UNSIGNED (type
) = 1;
649 TYPE_PRECISION (type
) = GET_MODE_BITSIZE (TYPE_MODE (type
));
654 register tree index
= TYPE_DOMAIN (type
);
655 register tree element
= TREE_TYPE (type
);
657 build_pointer_type (element
);
659 /* We need to know both bounds in order to compute the size. */
660 if (index
&& TYPE_MAX_VALUE (index
) && TYPE_MIN_VALUE (index
)
661 && TYPE_SIZE (element
))
664 = size_binop (PLUS_EXPR
, size_one_node
,
665 size_binop (MINUS_EXPR
, TYPE_MAX_VALUE (index
),
666 TYPE_MIN_VALUE (index
)));
668 TYPE_SIZE (type
) = size_binop (MULT_EXPR
, length
,
669 TYPE_SIZE (element
));
672 /* Now round the alignment and size,
673 using machine-dependent criteria if any. */
675 #ifdef ROUND_TYPE_ALIGN
677 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (element
), BITS_PER_UNIT
);
679 TYPE_ALIGN (type
) = MAX (TYPE_ALIGN (element
), BITS_PER_UNIT
);
682 #ifdef ROUND_TYPE_SIZE
683 if (TYPE_SIZE (type
) != 0)
685 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
688 TYPE_MODE (type
) = BLKmode
;
689 if (TYPE_SIZE (type
) != 0
690 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
691 /* BLKmode elements force BLKmode aggregate;
692 else extract/store fields may lose. */
693 && (TYPE_MODE (TREE_TYPE (type
)) != BLKmode
694 || TYPE_NO_FORCE_BLK (TREE_TYPE (type
))))
697 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
700 if (STRICT_ALIGNMENT
&& TYPE_ALIGN (type
) < BIGGEST_ALIGNMENT
701 && TYPE_ALIGN (type
) < TREE_INT_CST_LOW (TYPE_SIZE (type
))
702 && TYPE_MODE (type
) != BLKmode
)
704 TYPE_NO_FORCE_BLK (type
) = 1;
705 TYPE_MODE (type
) = BLKmode
;
712 pending_statics
= layout_record (type
);
713 TYPE_MODE (type
) = BLKmode
;
714 if (TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
717 /* A record which has any BLKmode members must itself be BLKmode;
718 it can't go in a register.
719 Unless the member is BLKmode only because it isn't aligned. */
720 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
724 if (TREE_CODE (field
) != FIELD_DECL
)
727 if (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
728 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
731 if (TREE_CODE (DECL_FIELD_BITPOS (field
)) != INTEGER_CST
)
734 bitpos
= TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field
));
736 /* Must be BLKmode if any field crosses a word boundary,
737 since extract_bit_field can't handle that in registers. */
738 if (bitpos
/ BITS_PER_WORD
739 != ((TREE_INT_CST_LOW (DECL_SIZE (field
)) + bitpos
- 1)
741 /* But there is no problem if the field is entire words. */
742 && TREE_INT_CST_LOW (DECL_SIZE (field
)) % BITS_PER_WORD
== 0)
747 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
750 /* If structure's known alignment is less than
751 what the scalar mode would need, and it matters,
752 then stick with BLKmode. */
754 && ! (TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
755 || (TYPE_ALIGN (type
)
756 >= TREE_INT_CST_LOW (TYPE_SIZE (type
)))))
758 if (TYPE_MODE (type
) != BLKmode
)
759 /* If this is the only reason this type is BLKmode,
760 then don't force containing types to be BLKmode. */
761 TYPE_NO_FORCE_BLK (type
) = 1;
762 TYPE_MODE (type
) = BLKmode
;
768 /* Lay out any static members. This is done now
769 because their type may use the record's type. */
770 while (pending_statics
)
772 layout_decl (TREE_VALUE (pending_statics
), 0);
773 pending_statics
= TREE_CHAIN (pending_statics
);
779 TYPE_MODE (type
) = BLKmode
;
780 if (TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
781 /* If structure's known alignment is less than
782 what the scalar mode would need, and it matters,
783 then stick with BLKmode. */
784 && (! STRICT_ALIGNMENT
785 || TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
786 || TYPE_ALIGN (type
) >= TREE_INT_CST_LOW (TYPE_SIZE (type
))))
789 /* A union 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
))
794 if (TREE_CODE (field
) != FIELD_DECL
)
797 if (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
798 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
803 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type
)),
814 /* Normally, use the alignment corresponding to the mode chosen.
815 However, where strict alignment is not required, avoid
816 over-aligning structures, since most compilers do not do this
819 if (TYPE_MODE (type
) != BLKmode
&& TYPE_MODE (type
) != VOIDmode
821 || (TREE_CODE (type
) != RECORD_TYPE
&& TREE_CODE (type
) != UNION_TYPE
822 && TREE_CODE (type
) != ARRAY_TYPE
)))
823 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
825 /* Evaluate nonconstant size only once, either now or as soon as safe. */
826 if (TYPE_SIZE (type
) != 0 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
827 TYPE_SIZE (type
) = variable_size (TYPE_SIZE (type
));
829 /* Also layout any other variants of the type. */
830 if (TYPE_NEXT_VARIANT (type
)
831 || type
!= TYPE_MAIN_VARIANT (type
))
834 /* Record layout info of this variant. */
835 tree size
= TYPE_SIZE (type
);
836 int align
= TYPE_ALIGN (type
);
837 enum machine_mode mode
= TYPE_MODE (type
);
839 /* Copy it into all variants. */
840 for (variant
= TYPE_MAIN_VARIANT (type
);
842 variant
= TYPE_NEXT_VARIANT (variant
))
844 TYPE_SIZE (variant
) = size
;
845 TYPE_ALIGN (variant
) = align
;
846 TYPE_MODE (variant
) = mode
;
851 resume_momentary (old
);
854 /* Create and return a type for signed integers of PRECISION bits. */
857 make_signed_type (precision
)
860 register tree type
= make_node (INTEGER_TYPE
);
862 TYPE_PRECISION (type
) = precision
;
864 /* Create the extreme values based on the number of bits. */
866 TYPE_MIN_VALUE (type
)
867 = build_int_2 ((precision
-HOST_BITS_PER_INT
> 0 ? 0 : (-1)<<(precision
-1)),
868 (-1)<<(precision
-HOST_BITS_PER_INT
-1 > 0
869 ? precision
-HOST_BITS_PER_INT
-1
871 TYPE_MAX_VALUE (type
)
872 = build_int_2 ((precision
-HOST_BITS_PER_INT
> 0 ? -1 : (1<<(precision
-1))-1),
873 (precision
-HOST_BITS_PER_INT
-1 > 0
874 ? (1<<(precision
-HOST_BITS_PER_INT
-1))-1
877 /* Give this type's extreme values this type as their type. */
879 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
880 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
882 /* The first type made with this or `make_unsigned_type'
883 is the type for size values. */
890 /* Lay out the type: set its alignment, size, etc. */
897 /* Create and return a type for unsigned integers of PRECISION bits. */
900 make_unsigned_type (precision
)
903 register tree type
= make_node (INTEGER_TYPE
);
905 TYPE_PRECISION (type
) = precision
;
907 /* The first type made with this or `make_signed_type'
908 is the type for size values. */
915 fixup_unsigned_type (type
);
919 /* Set the extreme values of TYPE based on its precision in bits,
920 the lay it out. This is used both in `make_unsigned_type'
921 and for enumeral types. */
924 fixup_unsigned_type (type
)
927 register int precision
= TYPE_PRECISION (type
);
929 TYPE_MIN_VALUE (type
) = build_int_2 (0, 0);
930 TYPE_MAX_VALUE (type
)
931 = build_int_2 (precision
-HOST_BITS_PER_INT
>= 0 ? -1 : (1<<precision
)-1,
932 precision
-HOST_BITS_PER_INT
> 0
934 >> (HOST_BITS_PER_INT
- (precision
- HOST_BITS_PER_INT
)))
936 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
937 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
939 /* Lay out the type: set its alignment, size, etc. */
944 /* Find the best machine mode to use when referencing a bit field of length
945 BITSIZE bits starting at BITPOS.
947 The underlying object is known to be aligned to a boundary of ALIGN bits.
948 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
949 larger than LARGEST_MODE (usually SImode).
951 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
952 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
953 mode meeting these conditions.
955 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), if a mode
956 whose size is UNITS_PER_WORD meets all the conditions, it is returned
960 get_best_mode (bitsize
, bitpos
, align
, largest_mode
, volatilep
)
963 enum machine_mode largest_mode
;
966 enum machine_mode mode
;
969 /* Find the narrowest integer mode that contains the bit field. */
970 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
971 mode
= GET_MODE_WIDER_MODE (mode
))
973 unit
= GET_MODE_BITSIZE (mode
);
974 if (bitpos
/ unit
== (bitpos
+ bitsize
- 1) / unit
)
978 if (mode
== MAX_MACHINE_MODE
979 /* It is tempting to omit the following line
980 if STRICT_ALIGNMENT is true.
981 But that is incorrect, since if the bitfield uses part of 3 bytes
982 and we use a 4-byte mode, we could get a spurious segv
983 if the extra 4th byte is past the end of memory.
984 (Though at least one Unix compiler ignores this problem:
985 that on the Sequent 386 machine. */
986 || MIN (unit
, BIGGEST_ALIGNMENT
) > align
987 || (largest_mode
!= VOIDmode
&& unit
> GET_MODE_BITSIZE (largest_mode
)))
992 && BITS_PER_WORD
<= MIN (align
, BIGGEST_ALIGNMENT
)
993 && (largest_mode
== VOIDmode
994 || BITS_PER_WORD
<= GET_MODE_BITSIZE (largest_mode
)))
1000 /* Save all variables describing the current status into the structure *P.
1001 This is used before starting a nested function. */
1004 save_storage_status (p
)
1007 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1008 p
->pending_sizes
= pending_sizes
;
1009 p
->immediate_size_expand
= immediate_size_expand
;
1013 /* Restore all variables describing the current status from the structure *P.
1014 This is used after a nested function. */
1017 restore_storage_status (p
)
1021 pending_sizes
= p
->pending_sizes
;
1022 immediate_size_expand
= p
->immediate_size_expand
;