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;
64 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
65 to serve as the actual size-expression for a type or decl. */
71 size = save_expr (size);
73 if (global_bindings_p ())
75 error ("variable-size type declared outside of any function");
79 if (immediate_size_expand)
80 expand_expr (size, 0, VOIDmode, 0);
82 pending_sizes = tree_cons (0, size, pending_sizes);
87 #ifndef MAX_FIXED_MODE_SIZE
88 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
91 /* Return the machine mode to use for a nonscalar of SIZE bits.
92 The mode must be in class CLASS, and have exactly that many bits.
93 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
97 mode_for_size (size, class, limit)
99 enum mode_class class;
102 register enum machine_mode mode;
104 if (limit && size > MAX_FIXED_MODE_SIZE)
107 /* Get the last mode which has this size, in the specified class. */
108 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
109 mode = GET_MODE_WIDER_MODE (mode))
110 if (GET_MODE_BITSIZE (mode) == size)
116 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
119 round_up (value, divisor)
123 return size_binop (MULT_EXPR,
124 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
128 /* Set the size, mode and alignment of a ..._DECL node.
129 TYPE_DECL does need this for C++.
130 Note that LABEL_DECL and CONST_DECL nodes do not need this,
131 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
132 Don't call layout_decl for them.
134 KNOWN_ALIGN is the amount of alignment we can assume this
135 decl has with no special effort. It is relevant only for FIELD_DECLs
136 and depends on the previous fields.
137 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
138 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
139 the record will be aligned to suit. */
142 layout_decl (decl, known_align)
144 unsigned known_align;
146 register tree type = TREE_TYPE (decl);
147 register enum tree_code code = TREE_CODE (decl);
148 int spec_size = DECL_FRAME_SIZE (decl);
150 if (code == CONST_DECL)
153 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
154 && code != FIELD_DECL && code != TYPE_DECL)
157 if (type == error_mark_node)
159 type = void_type_node;
163 /* Usually the size and mode come from the data type without change. */
165 DECL_MODE (decl) = TYPE_MODE (type);
166 DECL_SIZE (decl) = TYPE_SIZE (type);
167 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
169 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
171 /* This is a bit-field. We don't know how to handle
172 them except for integers and enums, and front end should
173 never generate them otherwise. */
175 if (! (TREE_CODE (type) == INTEGER_TYPE
176 || TREE_CODE (type) == ENUMERAL_TYPE))
179 if (spec_size == 0 && DECL_NAME (decl) != 0)
182 /* Size is specified number of bits. */
183 DECL_SIZE (decl) = size_int (spec_size);
185 /* Force alignment required for the data type.
186 But if the decl itself wants greater alignment, don't override that. */
187 else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
188 DECL_ALIGN (decl) = TYPE_ALIGN (type);
190 /* See if we can use an ordinary integer mode for a bit-field. */
191 /* Conditions are: a fixed size that is correct for another mode
192 and occupying a complete byte or bytes on proper boundary. */
193 if (code == FIELD_DECL)
194 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
195 if (DECL_BIT_FIELD (decl)
196 && TYPE_SIZE (type) != 0
197 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
199 register enum machine_mode xmode
200 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
203 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
205 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
207 DECL_MODE (decl) = xmode;
208 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
209 /* This no longer needs to be accessed as a bit field. */
210 DECL_BIT_FIELD (decl) = 0;
214 /* Evaluate nonconstant size only once, either now or as soon as safe. */
215 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
216 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
219 /* Lay out a RECORD_TYPE type (a C struct).
220 This means laying out the fields, determining their positions,
221 and computing the overall size and required alignment of the record.
222 Note that if you set the TYPE_ALIGN before calling this
223 then the struct is aligned to at least that boundary.
225 If the type has basetypes, you must call layout_basetypes
226 before calling this function.
228 The return value is a list of static members of the record.
229 They still need to be laid out. */
236 #ifdef STRUCTURE_SIZE_BOUNDARY
237 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
239 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
241 /* These must be laid out *after* the record is. */
242 tree pending_statics = NULL_TREE;
243 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
244 where CONST_SIZE is an integer
245 and VAR_SIZE is a tree expression.
246 If VAR_SIZE is null, the size is just CONST_SIZE.
247 Naturally we try to avoid using VAR_SIZE. */
248 register int const_size = 0;
249 register tree var_size = 0;
250 /* Once we start using VAR_SIZE, this is the maximum alignment
251 that we know VAR_SIZE has. */
252 register int var_align = BITS_PER_UNIT;
255 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
257 register int desired_align;
259 /* If FIELD is static, then treat it like a separate variable,
260 not really like a structure field.
261 If it is a FUNCTION_DECL, it's a method.
262 In both cases, all we do is lay out the decl,
263 and we do it *after* the record is laid out. */
265 if (TREE_STATIC (field))
267 pending_statics = tree_cons (NULL, field, pending_statics);
270 /* Enumerators and enum types which are local to this class need not
271 be laid out. Likewise for initialized constant fields. */
272 if (TREE_CODE (field) != FIELD_DECL)
275 /* Lay out the field so we know what alignment it needs.
276 For KNOWN_ALIGN, pass the number of bits from start of record
277 or some divisor of it. */
279 /* For a packed field, use the alignment as specified,
280 disregarding what the type would want. */
281 if (DECL_PACKED (field))
282 desired_align = DECL_ALIGN (field);
283 layout_decl (field, var_size ? var_align : const_size);
284 if (! DECL_PACKED (field))
285 desired_align = DECL_ALIGN (field);
286 /* Some targets (i.e. VMS) limit struct field alignment
287 to a lower boundary than alignment of variables. */
288 #ifdef BIGGEST_FIELD_ALIGNMENT
289 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
292 /* Record must have at least as much alignment as any field.
293 Otherwise, the alignment of the field within the record
296 #ifndef PCC_BITFIELD_TYPE_MATTERS
297 record_align = MAX (record_align, desired_align);
299 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
300 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
302 /* For these machines, a zero-length field does not
303 affect the alignment of the structure as a whole.
304 It does, however, affect the alignment of the next field
305 within the structure. */
306 if (! integer_zerop (DECL_SIZE (field)))
307 record_align = MAX (record_align, desired_align);
308 else if (! DECL_PACKED (field))
309 desired_align = TYPE_ALIGN (TREE_TYPE (field));
310 /* A named bit field of declared type `int'
311 forces the entire structure to have `int' alignment. */
312 if (DECL_NAME (field) != 0)
313 record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
316 record_align = MAX (record_align, desired_align);
319 /* Does this field automatically have alignment it needs
320 by virtue of the fields that precede it and the record's
323 if (const_size % desired_align != 0
324 || (var_align % desired_align != 0
327 /* No, we need to skip space before this field.
328 Bump the cumulative size to multiple of field alignment. */
331 || var_align % desired_align == 0)
333 = CEIL (const_size, desired_align) * desired_align;
337 var_size = size_binop (PLUS_EXPR, var_size, const_size);
339 var_size = round_up (var_size, desired_align);
340 var_align = MIN (var_align, desired_align);
344 #ifdef PCC_BITFIELD_TYPE_MATTERS
345 if (PCC_BITFIELD_TYPE_MATTERS
346 && TREE_CODE (field) == FIELD_DECL
347 && TREE_TYPE (field) != error_mark_node
348 && !DECL_PACKED (field)
349 && !integer_zerop (DECL_SIZE (field)))
351 int type_align = TYPE_ALIGN (TREE_TYPE (field));
352 register tree dsize = DECL_SIZE (field);
353 int field_size = TREE_INT_CST_LOW (dsize);
355 /* A bit field may not span the unit of alignment of its type.
356 Advance to next boundary if necessary. */
357 if (const_size / type_align
358 != (const_size + field_size - 1) / type_align)
359 const_size = CEIL (const_size, type_align) * type_align;
363 /* No existing machine description uses this parameter.
364 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
365 #ifdef BITFIELD_NBYTES_LIMITED
366 if (BITFIELD_NBYTES_LIMITED
367 && TREE_CODE (field) == FIELD_DECL
368 && TREE_TYPE (field) != error_mark_node
369 && !DECL_PACKED (field)
370 && !integer_zerop (DECL_SIZE (field)))
372 int type_align = TYPE_ALIGN (TREE_TYPE (field));
373 register tree dsize = DECL_SIZE (field);
374 int field_size = TREE_INT_CST_LOW (dsize);
376 /* A bit field may not span the unit of alignment of its type.
377 Advance to next boundary if necessary. */
378 if (const_size / type_align
379 != (const_size + field_size - 1) / type_align)
380 const_size = CEIL (const_size, type_align) * type_align;
384 /* Size so far becomes the position of this field. */
386 if (var_size && const_size)
387 DECL_FIELD_BITPOS (field)
388 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
390 DECL_FIELD_BITPOS (field) = var_size;
392 DECL_FIELD_BITPOS (field) = size_int (const_size);
394 /* If this field is an anonymous union,
395 give each union-member the same position as the union has. */
397 if (DECL_NAME (field) == 0
398 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
400 tree uelt = TYPE_FIELDS (TREE_TYPE (field));
401 for (; uelt; uelt = TREE_CHAIN (uelt))
403 DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
404 DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
408 /* Now add size of this field to the size of the record. */
411 register tree dsize = DECL_SIZE (field);
413 if (TREE_CODE (dsize) == INTEGER_CST)
414 const_size += TREE_INT_CST_LOW (dsize);
420 var_size = size_binop (PLUS_EXPR, var_size, dsize);
425 /* Work out the total size and alignment of the record
426 as one expression and store in the record type.
427 Round it up to a multiple of the record's alignment. */
431 TYPE_SIZE (rec) = size_int (const_size);
437 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
438 TYPE_SIZE (rec) = var_size;
441 /* Determine the desired alignment. */
442 #ifdef ROUND_TYPE_ALIGN
443 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
445 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
448 #ifdef ROUND_TYPE_SIZE
449 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
451 /* Round the size up to be a multiple of the required alignment */
452 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
455 return pending_statics;
458 /* Lay out a UNION_TYPE type.
459 Lay out all the fields, set their positions to zero,
460 and compute the size and alignment of the union (maximum of any field).
461 Note that if you set the TYPE_ALIGN before calling this
462 then the union align is aligned to at least that boundary. */
469 #ifdef STRUCTURE_SIZE_BOUNDARY
470 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
472 unsigned union_align = BITS_PER_UNIT;
475 /* The size of the union, based on the fields scanned so far,
476 is max (CONST_SIZE, VAR_SIZE).
477 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
478 register int const_size = 0;
479 register tree var_size = 0;
481 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
483 /* Enums which are local to this class need not be laid out. */
484 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
487 layout_decl (field, 0);
488 DECL_FIELD_BITPOS (field) = size_int (0);
490 /* Union must be at least as aligned as any field requires. */
492 union_align = MAX (union_align, DECL_ALIGN (field));
494 #ifdef PCC_BITFIELD_TYPE_MATTERS
495 /* On the m88000, a bit field of declare type `int'
496 forces the entire union to have `int' alignment. */
497 if (PCC_BITFIELD_TYPE_MATTERS)
498 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
501 /* Set union_size to max (decl_size, union_size).
502 There are more and less general ways to do this.
503 Use only CONST_SIZE unless forced to use VAR_SIZE. */
505 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
506 const_size = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
507 else if (var_size == 0)
508 var_size = DECL_SIZE (field);
510 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
513 /* Determine the ultimate size of the union (in bytes). */
514 if (NULL == var_size)
515 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
517 else if (const_size == 0)
518 TYPE_SIZE (rec) = var_size;
520 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
521 round_up (size_int (const_size),
524 /* Determine the desired alignment. */
525 #ifdef ROUND_TYPE_ALIGN
526 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
528 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
531 #ifdef ROUND_TYPE_SIZE
532 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
534 /* Round the size up to be a multiple of the required alignment */
535 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
539 /* Calculate the mode, size, and alignment for TYPE.
540 For an array type, calculate the element separation as well.
541 Record TYPE on the chain of permanent or temporary types
542 so that dbxout will find out about it.
544 TYPE_SIZE of a type is nonzero if the type has been laid out already.
545 layout_type does nothing on such a type.
547 If the type is incomplete, its TYPE_SIZE remains zero. */
554 tree pending_statics;
559 /* Do nothing if type has been laid out before. */
560 if (TYPE_SIZE (type))
563 /* Make sure all nodes we allocate are not momentary;
564 they must last past the current statement. */
565 old = suspend_momentary ();
567 /* If we are processing a permanent type, make nodes permanent.
568 If processing a temporary type, make it saveable, since the
569 type node itself is. This is important if the function is inline,
570 since its decls will get copied later. */
571 push_obstacks_nochange ();
572 if (allocation_temporary_p ())
574 if (TREE_PERMANENT (type))
575 end_temporary_allocation ();
577 saveable_allocation ();
580 switch (TREE_CODE (type))
583 /* This kind of type is the responsibility
584 of the languge-specific code. */
589 if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
590 TREE_UNSIGNED (type) = 1;
592 /* We pass 0 for the last arg of mode_for_size because otherwise
593 on the Apollo using long long causes a crash.
594 It seems better to use integer modes than to try to support
595 integer types with BLKmode. */
596 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_INT, 0);
597 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
601 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
602 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
606 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
608 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
609 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
610 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
612 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
616 TYPE_SIZE (type) = size_zero_node;
617 TYPE_ALIGN (type) = 1;
618 TYPE_MODE (type) = VOIDmode;
623 TYPE_MODE (type) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode),
625 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
630 TYPE_MODE (type) = Pmode;
631 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
632 TREE_UNSIGNED (type) = 1;
633 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
638 register tree index = TYPE_DOMAIN (type);
639 register tree element = TREE_TYPE (type);
641 build_pointer_type (element);
643 /* We need to know both bounds in order to compute the size. */
644 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
645 && TYPE_SIZE (element))
648 = size_binop (PLUS_EXPR, size_one_node,
649 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
650 TYPE_MIN_VALUE (index)));
652 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
653 TYPE_SIZE (element));
656 /* Now round the alignment and size,
657 using machine-dependent criteria if any. */
659 #ifdef ROUND_TYPE_ALIGN
661 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
663 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
666 #ifdef ROUND_TYPE_SIZE
667 if (TYPE_SIZE (type) != 0)
669 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
672 TYPE_MODE (type) = BLKmode;
673 if (TYPE_SIZE (type) != 0
674 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
675 /* BLKmode elements force BLKmode aggregate;
676 else extract/store fields may lose. */
677 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
678 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
681 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
684 #ifdef STRICT_ALIGNMENT
685 if (TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
686 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
687 && TYPE_MODE (type) != BLKmode)
689 TYPE_NO_FORCE_BLK (type) = 1;
690 TYPE_MODE (type) = BLKmode;
698 pending_statics = layout_record (type);
699 TYPE_MODE (type) = BLKmode;
700 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
703 /* A record which has any BLKmode members must itself be BLKmode;
704 it can't go in a register.
705 Unless the member is BLKmode only because it isn't aligned. */
706 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
710 if (TREE_CODE (field) != FIELD_DECL)
713 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
714 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
717 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
720 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
722 /* Must be BLKmode if any field crosses a word boundary,
723 since extract_bit_field can't handle that in registers. */
724 if (bitpos / BITS_PER_WORD
725 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
727 /* But there is no problem if the field is entire words. */
728 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
733 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
736 /* If structure's known alignment is less than
737 what the scalar mode would need, and it matters,
738 then stick with BLKmode. */
739 #ifdef STRICT_ALIGNMENT
740 if (! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
741 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
743 if (TYPE_MODE (type) != BLKmode)
744 /* If this is the only reason this type is BLKmode,
745 then don't force containing types to be BLKmode. */
746 TYPE_NO_FORCE_BLK (type) = 1;
747 TYPE_MODE (type) = BLKmode;
753 /* Lay out any static members. This is done now
754 because their type may use the record's type. */
755 while (pending_statics)
757 layout_decl (TREE_VALUE (pending_statics), 0);
758 pending_statics = TREE_CHAIN (pending_statics);
764 TYPE_MODE (type) = BLKmode;
765 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
766 /* If structure's known alignment is less than
767 what the scalar mode would need, and it matters,
768 then stick with BLKmode. */
769 #ifdef STRICT_ALIGNMENT
770 && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
771 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type)))
776 /* A union which has any BLKmode members must itself be BLKmode;
777 it can't go in a register.
778 Unless the member is BLKmode only because it isn't aligned. */
779 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
781 if (TREE_CODE (field) != FIELD_DECL)
784 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
785 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
790 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
801 /* Normally, use the alignment corresponding to the mode chosen.
802 However, where strict alignment is not required, avoid
803 over-aligning structures, since most compilers do not do this
806 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
807 #ifndef STRICT_ALIGNMENT
808 && (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
809 && TREE_CODE (type) != ARRAY_TYPE)
812 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
814 /* Evaluate nonconstant size only once, either now or as soon as safe. */
815 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
816 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
818 /* Also layout any other variants of the type. */
819 if (TYPE_NEXT_VARIANT (type)
820 || type != TYPE_MAIN_VARIANT (type))
823 /* Record layout info of this variant. */
824 tree size = TYPE_SIZE (type);
825 int align = TYPE_ALIGN (type);
826 enum machine_mode mode = TYPE_MODE (type);
828 /* Copy it into all variants. */
829 for (variant = TYPE_MAIN_VARIANT (type);
831 variant = TYPE_NEXT_VARIANT (variant))
833 TYPE_SIZE (variant) = size;
834 TYPE_ALIGN (variant) = align;
835 TYPE_MODE (variant) = mode;
840 resume_momentary (old);
843 /* Create and return a type for signed integers of PRECISION bits. */
846 make_signed_type (precision)
849 register tree type = make_node (INTEGER_TYPE);
851 TYPE_PRECISION (type) = precision;
853 /* Create the extreme values based on the number of bits. */
855 TYPE_MIN_VALUE (type)
856 = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
857 (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
858 ? precision-HOST_BITS_PER_INT-1
860 TYPE_MAX_VALUE (type)
861 = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
862 (precision-HOST_BITS_PER_INT-1 > 0
863 ? (1<<(precision-HOST_BITS_PER_INT-1))-1
866 /* Give this type's extreme values this type as their type. */
868 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
869 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
871 /* The first type made with this or `make_unsigned_type'
872 is the type for size values. */
879 /* Lay out the type: set its alignment, size, etc. */
886 /* Create and return a type for unsigned integers of PRECISION bits. */
889 make_unsigned_type (precision)
892 register tree type = make_node (INTEGER_TYPE);
894 TYPE_PRECISION (type) = precision;
896 /* The first type made with this or `make_signed_type'
897 is the type for size values. */
904 fixup_unsigned_type (type);
908 /* Set the extreme values of TYPE based on its precision in bits,
909 the lay it out. This is used both in `make_unsigned_type'
910 and for enumeral types. */
913 fixup_unsigned_type (type)
916 register int precision = TYPE_PRECISION (type);
918 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
919 TYPE_MAX_VALUE (type)
920 = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
921 precision-HOST_BITS_PER_INT > 0
923 >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
925 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
926 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
928 /* Lay out the type: set its alignment, size, etc. */
933 /* Find the best machine mode to use when referencing a bit field of length
934 BITSIZE bits starting at BITPOS.
936 The underlying object is known to be aligned to a boundary of ALIGN bits.
937 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
938 larger than LARGEST_MODE (usually SImode).
940 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
941 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
942 mode meeting these conditions.
944 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), if a mode
945 whose size is UNITS_PER_WORD meets all the conditions, it is returned
949 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
952 enum machine_mode largest_mode;
955 enum machine_mode mode;
958 /* Find the narrowest integer mode that contains the bit field. */
959 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
960 mode = GET_MODE_WIDER_MODE (mode))
962 unit = GET_MODE_BITSIZE (mode);
963 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
967 if (mode == MAX_MACHINE_MODE
968 /* It is tempting to omit the following line
969 if STRICT_ALIGNMENT is not defined.
970 But that is incorrect, since if the bitfield uses part of 3 bytes
971 and we use a 4-byte mode, we could get a spurious segv
972 if the extra 4th byte is past the end of memory.
973 (Though at least one Unix compiler ignores this problem:
974 that on the Sequent 386 machine. */
975 || MIN (unit, BIGGEST_ALIGNMENT) > align
976 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
981 && BITS_PER_WORD <= MIN (align, BIGGEST_ALIGNMENT)
982 && (largest_mode == VOIDmode
983 || BITS_PER_WORD <= GET_MODE_BITSIZE (largest_mode)))
989 /* Save all variables describing the current status into the structure *P.
990 This is used before starting a nested function. */
993 save_storage_status (p)
996 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
997 p->pending_sizes = pending_sizes;
998 p->immediate_size_expand = immediate_size_expand;
1002 /* Restore all variables describing the current status from the structure *P.
1003 This is used after a nested function. */
1006 restore_storage_status (p)
1010 pending_sizes = p->pending_sizes;
1011 immediate_size_expand = p->immediate_size_expand;