1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 88, 92-97, 1998 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
33 #define CEIL(x,y) (((x) + (y) - 1) / (y))
35 /* Data type for the expressions representing sizes of data types.
36 It is the first integer type laid out. */
38 struct sizetype_tab sizetype_tab;
40 /* An integer constant with value 0 whose type is sizetype. */
44 /* An integer constant with value 1 whose type is sizetype. */
48 /* If nonzero, this is an upper limit on alignment of structure fields.
49 The value is measured in bits. */
50 int maximum_field_alignment;
52 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
53 May be overridden by front-ends. */
54 int set_alignment = 0;
56 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
58 static tree layout_record PROTO((tree));
59 static void layout_union PROTO((tree));
61 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
63 static tree pending_sizes;
65 /* Nonzero means cannot safely call expand_expr now,
66 so put variable sizes onto `pending_sizes' instead. */
68 int immediate_size_expand;
73 tree chain = pending_sizes;
76 /* Put each SAVE_EXPR into the current function. */
77 for (t = chain; t; t = TREE_CHAIN (t))
78 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
84 put_pending_sizes (chain)
90 pending_sizes = chain;
93 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
94 to serve as the actual size-expression for a type or decl. */
100 /* If the language-processor is to take responsibility for variable-sized
101 items (e.g., languages which have elaboration procedures like Ada),
102 just return SIZE unchanged. Likewise for self-referential sizes. */
103 if (TREE_CONSTANT (size)
104 || global_bindings_p () < 0 || contains_placeholder_p (size))
107 size = save_expr (size);
109 if (global_bindings_p ())
111 if (TREE_CONSTANT (size))
112 error ("type size can't be explicitly evaluated");
114 error ("variable-size type declared outside of any function");
119 if (immediate_size_expand)
120 /* NULL_RTX is not defined; neither is the rtx type.
121 Also, we would like to pass const0_rtx here, but don't have it. */
122 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
125 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
130 #ifndef MAX_FIXED_MODE_SIZE
131 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
134 /* Return the machine mode to use for a nonscalar of SIZE bits.
135 The mode must be in class CLASS, and have exactly that many bits.
136 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
140 mode_for_size (size, class, limit)
142 enum mode_class class;
145 register enum machine_mode mode;
147 if (limit && size > MAX_FIXED_MODE_SIZE)
150 /* Get the first mode which has this size, in the specified class. */
151 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
152 mode = GET_MODE_WIDER_MODE (mode))
153 if (GET_MODE_BITSIZE (mode) == size)
159 /* Similar, but never return BLKmode; return the narrowest mode that
160 contains at least the requested number of bits. */
162 static enum machine_mode
163 smallest_mode_for_size (size, class)
165 enum mode_class class;
167 register enum machine_mode mode;
169 /* Get the first mode which has at least this size, in the
171 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
172 mode = GET_MODE_WIDER_MODE (mode))
173 if (GET_MODE_BITSIZE (mode) >= size)
179 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
182 round_up (value, divisor)
186 return size_binop (MULT_EXPR,
187 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
191 /* Set the size, mode and alignment of a ..._DECL node.
192 TYPE_DECL does need this for C++.
193 Note that LABEL_DECL and CONST_DECL nodes do not need this,
194 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
195 Don't call layout_decl for them.
197 KNOWN_ALIGN is the amount of alignment we can assume this
198 decl has with no special effort. It is relevant only for FIELD_DECLs
199 and depends on the previous fields.
200 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
201 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
202 the record will be aligned to suit. */
205 layout_decl (decl, known_align)
207 unsigned known_align;
209 register tree type = TREE_TYPE (decl);
210 register enum tree_code code = TREE_CODE (decl);
211 int spec_size = DECL_FIELD_SIZE (decl);
213 if (code == CONST_DECL)
216 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
217 && code != FIELD_DECL && code != TYPE_DECL)
220 if (type == error_mark_node)
222 type = void_type_node;
226 /* Usually the size and mode come from the data type without change. */
228 DECL_MODE (decl) = TYPE_MODE (type);
229 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
230 if (DECL_SIZE (decl) == 0)
231 DECL_SIZE (decl) = TYPE_SIZE (type);
233 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
235 if (spec_size == 0 && DECL_NAME (decl) != 0)
238 /* Size is specified number of bits. */
239 DECL_SIZE (decl) = size_int (spec_size);
241 /* Force alignment required for the data type.
242 But if the decl itself wants greater alignment, don't override that.
243 Likewise, if the decl is packed, don't override it. */
244 else if (DECL_ALIGN (decl) == 0
245 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
246 DECL_ALIGN (decl) = TYPE_ALIGN (type);
248 /* See if we can use an ordinary integer mode for a bit-field. */
249 /* Conditions are: a fixed size that is correct for another mode
250 and occupying a complete byte or bytes on proper boundary. */
251 if (code == FIELD_DECL)
253 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
254 if (maximum_field_alignment != 0)
255 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
256 else if (DECL_PACKED (decl))
257 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
260 if (DECL_BIT_FIELD (decl)
261 && TYPE_SIZE (type) != 0
262 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
263 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
265 register enum machine_mode xmode
266 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
269 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
271 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
273 DECL_MODE (decl) = xmode;
274 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
275 /* This no longer needs to be accessed as a bit field. */
276 DECL_BIT_FIELD (decl) = 0;
280 /* Turn off DECL_BIT_FIELD if we won't need it set. */
281 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
282 && known_align % TYPE_ALIGN (type) == 0
283 && DECL_SIZE (decl) != 0
284 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
285 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
286 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
287 DECL_BIT_FIELD (decl) = 0;
289 /* Evaluate nonconstant size only once, either now or as soon as safe. */
290 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
291 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
294 /* Lay out a RECORD_TYPE type (a C struct).
295 This means laying out the fields, determining their positions,
296 and computing the overall size and required alignment of the record.
297 Note that if you set the TYPE_ALIGN before calling this
298 then the struct is aligned to at least that boundary.
300 If the type has basetypes, you must call layout_basetypes
301 before calling this function.
303 The return value is a list of static members of the record.
304 They still need to be laid out. */
311 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
312 /* These must be laid out *after* the record is. */
313 tree pending_statics = NULL_TREE;
314 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
315 where CONST_SIZE is an integer
316 and VAR_SIZE is a tree expression.
317 If VAR_SIZE is null, the size is just CONST_SIZE.
318 Naturally we try to avoid using VAR_SIZE. */
319 register HOST_WIDE_INT const_size = 0;
320 register tree var_size = 0;
321 /* Once we start using VAR_SIZE, this is the maximum alignment
322 that we know VAR_SIZE has. */
323 register int var_align = BITS_PER_UNIT;
325 #ifdef STRUCTURE_SIZE_BOUNDARY
326 /* Packed structures don't need to have minimum size. */
327 if (! TYPE_PACKED (rec))
328 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
331 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
333 register int known_align = var_size ? var_align : const_size;
334 register int desired_align = 0;
336 /* If FIELD is static, then treat it like a separate variable,
337 not really like a structure field.
338 If it is a FUNCTION_DECL, it's a method.
339 In both cases, all we do is lay out the decl,
340 and we do it *after* the record is laid out. */
342 if (TREE_CODE (field) == VAR_DECL)
344 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
347 /* Enumerators and enum types which are local to this class need not
348 be laid out. Likewise for initialized constant fields. */
349 if (TREE_CODE (field) != FIELD_DECL)
352 /* Lay out the field so we know what alignment it needs.
353 For a packed field, use the alignment as specified,
354 disregarding what the type would want. */
355 if (DECL_PACKED (field))
356 desired_align = DECL_ALIGN (field);
357 layout_decl (field, known_align);
358 if (! DECL_PACKED (field))
359 desired_align = DECL_ALIGN (field);
360 /* Some targets (i.e. VMS) limit struct field alignment
361 to a lower boundary than alignment of variables. */
362 #ifdef BIGGEST_FIELD_ALIGNMENT
363 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
365 #ifdef ADJUST_FIELD_ALIGN
366 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
369 /* Record must have at least as much alignment as any field.
370 Otherwise, the alignment of the field within the record
373 #ifndef PCC_BITFIELD_TYPE_MATTERS
374 record_align = MAX (record_align, desired_align);
376 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
377 && DECL_BIT_FIELD_TYPE (field)
378 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
380 /* For these machines, a zero-length field does not
381 affect the alignment of the structure as a whole.
382 It does, however, affect the alignment of the next field
383 within the structure. */
384 if (! integer_zerop (DECL_SIZE (field)))
385 record_align = MAX (record_align, desired_align);
386 else if (! DECL_PACKED (field))
387 desired_align = TYPE_ALIGN (TREE_TYPE (field));
388 /* A named bit field of declared type `int'
389 forces the entire structure to have `int' alignment. */
390 if (DECL_NAME (field) != 0)
392 int type_align = TYPE_ALIGN (TREE_TYPE (field));
393 if (maximum_field_alignment != 0)
394 type_align = MIN (type_align, maximum_field_alignment);
395 else if (DECL_PACKED (field))
396 type_align = MIN (type_align, BITS_PER_UNIT);
398 record_align = MAX (record_align, type_align);
402 record_align = MAX (record_align, desired_align);
405 /* Does this field automatically have alignment it needs
406 by virtue of the fields that precede it and the record's
409 if (const_size % desired_align != 0
410 || (var_align % desired_align != 0
413 /* No, we need to skip space before this field.
414 Bump the cumulative size to multiple of field alignment. */
417 || var_align % desired_align == 0)
419 = CEIL (const_size, desired_align) * desired_align;
423 var_size = size_binop (PLUS_EXPR, var_size,
424 bitsize_int (const_size, 0L));
426 var_size = round_up (var_size, desired_align);
427 var_align = MIN (var_align, desired_align);
431 #ifdef PCC_BITFIELD_TYPE_MATTERS
432 if (PCC_BITFIELD_TYPE_MATTERS
433 && TREE_CODE (field) == FIELD_DECL
434 && TREE_TYPE (field) != error_mark_node
435 && DECL_BIT_FIELD_TYPE (field)
436 && !DECL_PACKED (field)
437 && maximum_field_alignment == 0
438 && !integer_zerop (DECL_SIZE (field)))
440 int type_align = TYPE_ALIGN (TREE_TYPE (field));
441 register tree dsize = DECL_SIZE (field);
442 int field_size = TREE_INT_CST_LOW (dsize);
444 /* A bit field may not span more units of alignment of its type
445 than its type itself. Advance to next boundary if necessary. */
446 if (((const_size + field_size + type_align - 1) / type_align
447 - const_size / type_align)
448 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
449 const_size = CEIL (const_size, type_align) * type_align;
453 /* No existing machine description uses this parameter.
454 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
455 #ifdef BITFIELD_NBYTES_LIMITED
456 if (BITFIELD_NBYTES_LIMITED
457 && TREE_CODE (field) == FIELD_DECL
458 && TREE_TYPE (field) != error_mark_node
459 && DECL_BIT_FIELD_TYPE (field)
460 && !DECL_PACKED (field)
461 && !integer_zerop (DECL_SIZE (field)))
463 int type_align = TYPE_ALIGN (TREE_TYPE (field));
464 register tree dsize = DECL_SIZE (field);
465 int field_size = TREE_INT_CST_LOW (dsize);
467 if (maximum_field_alignment != 0)
468 type_align = MIN (type_align, maximum_field_alignment);
469 /* ??? This test is opposite the test in the containing if
470 statement, so this code is unreachable currently. */
471 else if (DECL_PACKED (field))
472 type_align = MIN (type_align, BITS_PER_UNIT);
474 /* A bit field may not span the unit of alignment of its type.
475 Advance to next boundary if necessary. */
476 /* ??? This code should match the code above for the
477 PCC_BITFIELD_TYPE_MATTERS case. */
478 if (const_size / type_align
479 != (const_size + field_size - 1) / type_align)
480 const_size = CEIL (const_size, type_align) * type_align;
484 /* Size so far becomes the position of this field. */
486 if (var_size && const_size)
487 DECL_FIELD_BITPOS (field)
488 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
490 DECL_FIELD_BITPOS (field) = var_size;
493 DECL_FIELD_BITPOS (field) = size_int (const_size);
495 /* If this field ended up more aligned than we thought it
496 would be (we approximate this by seeing if its position
497 changed), lay out the field again; perhaps we can use an
498 integral mode for it now. */
499 if (known_align != const_size)
500 layout_decl (field, const_size);
503 /* Now add size of this field to the size of the record. */
506 register tree dsize = DECL_SIZE (field);
508 /* This can happen when we have an invalid nested struct definition,
509 such as struct j { struct j { int i; } }. The error message is
510 printed in finish_struct. */
513 else if (TREE_CODE (dsize) == INTEGER_CST
514 && ! TREE_CONSTANT_OVERFLOW (dsize)
515 && TREE_INT_CST_HIGH (dsize) == 0
516 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
517 /* Use const_size if there's no overflow. */
518 const_size += TREE_INT_CST_LOW (dsize);
524 var_size = size_binop (PLUS_EXPR, var_size, dsize);
529 /* Work out the total size and alignment of the record
530 as one expression and store in the record type.
531 Round it up to a multiple of the record's alignment. */
535 TYPE_SIZE (rec) = size_int (const_size);
541 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
542 TYPE_SIZE (rec) = var_size;
545 /* Determine the desired alignment. */
546 #ifdef ROUND_TYPE_ALIGN
547 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
549 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
552 #ifdef ROUND_TYPE_SIZE
553 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
555 /* Round the size up to be a multiple of the required alignment */
556 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
559 return pending_statics;
562 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
563 Lay out all the fields, set their positions to zero,
564 and compute the size and alignment of the union (maximum of any field).
565 Note that if you set the TYPE_ALIGN before calling this
566 then the union align is aligned to at least that boundary. */
573 unsigned union_align = BITS_PER_UNIT;
575 /* The size of the union, based on the fields scanned so far,
576 is max (CONST_SIZE, VAR_SIZE).
577 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
578 register int const_size = 0;
579 register tree var_size = 0;
581 #ifdef STRUCTURE_SIZE_BOUNDARY
582 /* Packed structures don't need to have minimum size. */
583 if (! TYPE_PACKED (rec))
584 union_align = STRUCTURE_SIZE_BOUNDARY;
587 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
588 the reverse order in building the COND_EXPR that denotes its
589 size. We reverse them again later. */
590 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
591 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
593 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
595 /* Enums which are local to this class need not be laid out. */
596 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
599 layout_decl (field, 0);
600 DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
602 /* Union must be at least as aligned as any field requires. */
604 union_align = MAX (union_align, DECL_ALIGN (field));
606 #ifdef PCC_BITFIELD_TYPE_MATTERS
607 /* On the m88000, a bit field of declare type `int'
608 forces the entire union to have `int' alignment. */
609 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
610 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
613 if (TREE_CODE (rec) == UNION_TYPE)
615 /* Set union_size to max (decl_size, union_size).
616 There are more and less general ways to do this.
617 Use only CONST_SIZE unless forced to use VAR_SIZE. */
619 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
621 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
622 else if (var_size == 0)
623 var_size = DECL_SIZE (field);
625 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
627 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
628 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
630 var_size ? var_size : bitsize_int (0L, 0L)));
633 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
634 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
636 /* Determine the ultimate size of the union (in bytes). */
637 if (NULL == var_size)
638 TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
639 * BITS_PER_UNIT, 0L);
640 else if (const_size == 0)
641 TYPE_SIZE (rec) = var_size;
643 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
644 round_up (bitsize_int (const_size, 0L),
647 /* Determine the desired alignment. */
648 #ifdef ROUND_TYPE_ALIGN
649 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
651 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
654 #ifdef ROUND_TYPE_SIZE
655 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
657 /* Round the size up to be a multiple of the required alignment */
658 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
662 /* Calculate the mode, size, and alignment for TYPE.
663 For an array type, calculate the element separation as well.
664 Record TYPE on the chain of permanent or temporary types
665 so that dbxout will find out about it.
667 TYPE_SIZE of a type is nonzero if the type has been laid out already.
668 layout_type does nothing on such a type.
670 If the type is incomplete, its TYPE_SIZE remains zero. */
677 tree pending_statics;
682 /* Do nothing if type has been laid out before. */
683 if (TYPE_SIZE (type))
686 /* Make sure all nodes we allocate are not momentary;
687 they must last past the current statement. */
688 old = suspend_momentary ();
690 /* Put all our nodes into the same obstack as the type. Also,
691 make expressions saveable (this is a no-op for permanent types). */
693 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
694 saveable_allocation ();
696 switch (TREE_CODE (type))
699 /* This kind of type is the responsibility
700 of the language-specific code. */
703 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
704 if (TYPE_PRECISION (type) == 0)
705 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
706 /* ... fall through ... */
711 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
712 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
713 TREE_UNSIGNED (type) = 1;
715 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
717 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
721 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
722 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
726 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
728 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
729 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
730 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
732 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
736 TYPE_SIZE (type) = size_zero_node;
737 TYPE_ALIGN (type) = 1;
738 TYPE_MODE (type) = VOIDmode;
742 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
743 TYPE_MODE (type) = ptr_mode;
748 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
749 TYPE_SIZE (type) = size_int (2 * POINTER_SIZE);
754 TYPE_MODE (type) = ptr_mode;
755 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
756 TREE_UNSIGNED (type) = 1;
757 TYPE_PRECISION (type) = POINTER_SIZE;
762 register tree index = TYPE_DOMAIN (type);
763 register tree element = TREE_TYPE (type);
765 build_pointer_type (element);
767 /* We need to know both bounds in order to compute the size. */
768 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
769 && TYPE_SIZE (element))
771 tree ub = TYPE_MAX_VALUE (index);
772 tree lb = TYPE_MIN_VALUE (index);
775 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
776 test for negative below covers it. */
777 if (TREE_CODE (ub) == MAX_EXPR
778 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
779 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
780 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
782 ub = TREE_OPERAND (ub, 1);
783 else if (TREE_CODE (ub) == MAX_EXPR
784 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
785 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
786 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
789 ub = TREE_OPERAND (ub, 0);
791 /* The initial subtraction should happen in the original type so
792 that (possible) negative values are handled appropriately. */
793 length = size_binop (PLUS_EXPR, size_one_node,
794 fold (build (MINUS_EXPR, TREE_TYPE (lb),
797 /* If neither bound is a constant and sizetype is signed, make
798 sure the size is never negative. We should really do this
799 if *either* bound is non-constant, but this is the best
800 compromise between C and Ada. */
801 if (! TREE_UNSIGNED (sizetype)
802 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
803 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
804 length = size_binop (MAX_EXPR, length, size_zero_node);
806 TYPE_SIZE (type) = size_binop (MULT_EXPR, TYPE_SIZE (element),
810 /* Now round the alignment and size,
811 using machine-dependent criteria if any. */
813 #ifdef ROUND_TYPE_ALIGN
815 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
817 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
820 #ifdef ROUND_TYPE_SIZE
821 if (TYPE_SIZE (type) != 0)
823 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
826 TYPE_MODE (type) = BLKmode;
827 if (TYPE_SIZE (type) != 0
828 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
829 /* BLKmode elements force BLKmode aggregate;
830 else extract/store fields may lose. */
831 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
832 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
835 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
838 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
839 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
840 && TYPE_MODE (type) != BLKmode)
842 TYPE_NO_FORCE_BLK (type) = 1;
843 TYPE_MODE (type) = BLKmode;
850 pending_statics = layout_record (type);
851 TYPE_MODE (type) = BLKmode;
852 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
855 enum machine_mode mode = VOIDmode;
857 /* A record which has any BLKmode members must itself be BLKmode;
858 it can't go in a register.
859 Unless the member is BLKmode only because it isn't aligned. */
860 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
864 if (TREE_CODE (field) != FIELD_DECL)
867 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
868 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
871 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
874 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
876 /* Must be BLKmode if any field crosses a word boundary,
877 since extract_bit_field can't handle that in registers. */
878 if (bitpos / BITS_PER_WORD
879 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
881 /* But there is no problem if the field is entire words. */
882 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
885 /* If this field is the whole struct, remember its mode so
886 that, say, we can put a double in a class into a DF
887 register instead of forcing it to live in the stack. */
888 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
889 mode = DECL_MODE (field);
892 if (mode != VOIDmode)
893 /* We only have one real field; use its mode. */
894 TYPE_MODE (type) = mode;
897 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
900 /* If structure's known alignment is less than
901 what the scalar mode would need, and it matters,
902 then stick with BLKmode. */
904 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
905 || (TYPE_ALIGN (type)
906 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
908 if (TYPE_MODE (type) != BLKmode)
909 /* If this is the only reason this type is BLKmode,
910 then don't force containing types to be BLKmode. */
911 TYPE_NO_FORCE_BLK (type) = 1;
912 TYPE_MODE (type) = BLKmode;
918 /* Lay out any static members. This is done now
919 because their type may use the record's type. */
920 while (pending_statics)
922 layout_decl (TREE_VALUE (pending_statics), 0);
923 pending_statics = TREE_CHAIN (pending_statics);
928 case QUAL_UNION_TYPE:
930 TYPE_MODE (type) = BLKmode;
931 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
932 /* If structure's known alignment is less than
933 what the scalar mode would need, and it matters,
934 then stick with BLKmode. */
935 && (! STRICT_ALIGNMENT
936 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
937 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
940 /* A union which has any BLKmode members must itself be BLKmode;
941 it can't go in a register.
942 Unless the member is BLKmode only because it isn't aligned. */
943 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
945 if (TREE_CODE (field) != FIELD_DECL)
948 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
949 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
954 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
961 case SET_TYPE: /* Used by Chill and Pascal. */
962 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
963 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
967 #ifndef SET_WORD_SIZE
968 #define SET_WORD_SIZE BITS_PER_WORD
970 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
972 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
973 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
975 = ((size_in_bits + alignment - 1) / alignment) * alignment;
976 if (rounded_size > alignment)
977 TYPE_MODE (type) = BLKmode;
979 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
980 TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
981 TYPE_ALIGN (type) = alignment;
982 TYPE_PRECISION (type) = size_in_bits;
987 /* The size may vary in different languages, so the language front end
988 should fill in the size. */
989 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
990 TYPE_MODE (type) = BLKmode;
997 /* Normally, use the alignment corresponding to the mode chosen.
998 However, where strict alignment is not required, avoid
999 over-aligning structures, since most compilers do not do this
1002 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1003 && (STRICT_ALIGNMENT
1004 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1005 && TREE_CODE (type) != QUAL_UNION_TYPE
1006 && TREE_CODE (type) != ARRAY_TYPE)))
1007 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1009 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1010 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1011 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1013 /* Also layout any other variants of the type. */
1014 if (TYPE_NEXT_VARIANT (type)
1015 || type != TYPE_MAIN_VARIANT (type))
1018 /* Record layout info of this variant. */
1019 tree size = TYPE_SIZE (type);
1020 int align = TYPE_ALIGN (type);
1021 enum machine_mode mode = TYPE_MODE (type);
1023 /* Copy it into all variants. */
1024 for (variant = TYPE_MAIN_VARIANT (type);
1026 variant = TYPE_NEXT_VARIANT (variant))
1028 TYPE_SIZE (variant) = size;
1029 TYPE_ALIGN (variant) = align;
1030 TYPE_MODE (variant) = mode;
1035 resume_momentary (old);
1038 /* Create and return a type for signed integers of PRECISION bits. */
1041 make_signed_type (precision)
1044 register tree type = make_node (INTEGER_TYPE);
1046 TYPE_PRECISION (type) = precision;
1048 /* Create the extreme values based on the number of bits. */
1050 TYPE_MIN_VALUE (type)
1051 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1052 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1053 (((HOST_WIDE_INT) (-1)
1054 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1055 ? precision - HOST_BITS_PER_WIDE_INT - 1
1057 TYPE_MAX_VALUE (type)
1058 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1059 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1060 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1061 ? (((HOST_WIDE_INT) 1
1062 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1065 /* Give this type's extreme values this type as their type. */
1067 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1068 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1070 /* The first type made with this or `make_unsigned_type'
1071 is the type for size values. */
1074 set_sizetype (type);
1076 /* Lay out the type: set its alignment, size, etc. */
1083 /* Create and return a type for unsigned integers of PRECISION bits. */
1086 make_unsigned_type (precision)
1089 register tree type = make_node (INTEGER_TYPE);
1091 TYPE_PRECISION (type) = precision;
1093 /* The first type made with this or `make_signed_type'
1094 is the type for size values. */
1098 TREE_UNSIGNED (type) = 1;
1099 set_sizetype (type);
1102 fixup_unsigned_type (type);
1106 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1107 Also update the type of any standard type's sizes made so far. */
1113 int oprecision = TYPE_PRECISION (type), precision;
1117 /* The *bitsizetype types use a precision that avoids overflows when
1118 calculating signed sizes / offsets in bits.
1120 We are allocating bitsizetype once and change it in place when
1121 we decide later that we want to change it. This way, we avoid the
1122 hassle of changing all the TYPE_SIZE (TREE_TYPE (sometype))
1123 individually in each front end. */
1125 bitsizetype = make_node (INTEGER_TYPE);
1127 precision = oprecision + BITS_PER_UNIT_LOG + 1;
1128 /* However, when cross-compiling from a 32 bit to a 64 bit host,
1129 we are limited to 64 bit precision. */
1130 if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1131 precision = 2 * HOST_BITS_PER_WIDE_INT;
1132 TYPE_PRECISION (bitsizetype) = precision;
1133 if (TREE_UNSIGNED (type))
1134 fixup_unsigned_type (bitsizetype);
1136 fixup_signed_type (bitsizetype);
1137 layout_type (bitsizetype);
1139 if (TREE_UNSIGNED (type))
1141 usizetype = sizetype;
1142 ubitsizetype = bitsizetype;
1143 ssizetype = make_signed_type (oprecision);
1144 sbitsizetype = make_signed_type (precision);
1148 ssizetype = sizetype;
1149 sbitsizetype = bitsizetype;
1150 usizetype = make_unsigned_type (oprecision);
1151 ubitsizetype = make_unsigned_type (precision);
1155 /* Set the extreme values of TYPE based on its precision in bits,
1156 then lay it out. Used when make_signed_type won't do
1157 because the tree code is not INTEGER_TYPE.
1158 E.g. for Pascal, when the -fsigned-char option is given. */
1161 fixup_signed_type (type)
1164 register int precision = TYPE_PRECISION (type);
1166 TYPE_MIN_VALUE (type)
1167 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1168 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1169 (((HOST_WIDE_INT) (-1)
1170 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1171 ? precision - HOST_BITS_PER_WIDE_INT - 1
1173 TYPE_MAX_VALUE (type)
1174 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1175 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1176 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1177 ? (((HOST_WIDE_INT) 1
1178 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1181 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1182 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1184 /* Lay out the type: set its alignment, size, etc. */
1189 /* Set the extreme values of TYPE based on its precision in bits,
1190 then lay it out. This is used both in `make_unsigned_type'
1191 and for enumeral types. */
1194 fixup_unsigned_type (type)
1197 register int precision = TYPE_PRECISION (type);
1199 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1200 TYPE_MAX_VALUE (type)
1201 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1202 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1203 precision - HOST_BITS_PER_WIDE_INT > 0
1204 ? ((unsigned HOST_WIDE_INT) ~0
1205 >> (HOST_BITS_PER_WIDE_INT
1206 - (precision - HOST_BITS_PER_WIDE_INT)))
1208 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1209 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1211 /* Lay out the type: set its alignment, size, etc. */
1216 /* Find the best machine mode to use when referencing a bit field of length
1217 BITSIZE bits starting at BITPOS.
1219 The underlying object is known to be aligned to a boundary of ALIGN bits.
1220 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1221 larger than LARGEST_MODE (usually SImode).
1223 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1224 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1225 mode meeting these conditions.
1227 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1228 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1229 all the conditions. */
1232 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1233 int bitsize, bitpos;
1235 enum machine_mode largest_mode;
1238 enum machine_mode mode;
1241 /* Find the narrowest integer mode that contains the bit field. */
1242 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1243 mode = GET_MODE_WIDER_MODE (mode))
1245 unit = GET_MODE_BITSIZE (mode);
1246 if ((bitpos % unit) + bitsize <= unit)
1250 if (mode == MAX_MACHINE_MODE
1251 /* It is tempting to omit the following line
1252 if STRICT_ALIGNMENT is true.
1253 But that is incorrect, since if the bitfield uses part of 3 bytes
1254 and we use a 4-byte mode, we could get a spurious segv
1255 if the extra 4th byte is past the end of memory.
1256 (Though at least one Unix compiler ignores this problem:
1257 that on the Sequent 386 machine. */
1258 || MIN (unit, BIGGEST_ALIGNMENT) > align
1259 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1262 if (SLOW_BYTE_ACCESS && ! volatilep)
1264 enum machine_mode wide_mode = VOIDmode, tmode;
1266 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1267 tmode = GET_MODE_WIDER_MODE (tmode))
1269 unit = GET_MODE_BITSIZE (tmode);
1270 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1271 && unit <= BITS_PER_WORD
1272 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1273 && (largest_mode == VOIDmode
1274 || unit <= GET_MODE_BITSIZE (largest_mode)))
1278 if (wide_mode != VOIDmode)
1285 /* Save all variables describing the current status into the structure *P.
1286 This is used before starting a nested function. */
1289 save_storage_status (p)
1292 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1293 p->pending_sizes = pending_sizes;
1294 p->immediate_size_expand = immediate_size_expand;
1298 /* Restore all variables describing the current status from the structure *P.
1299 This is used after a nested function. */
1302 restore_storage_status (p)
1306 pending_sizes = p->pending_sizes;
1307 immediate_size_expand = p->immediate_size_expand;