include tree.h
[official-gcc.git] / gcc / stor-layout.c
blobd2c6f2824f98264d2679af8f00e6d79770c4c56b
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
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. */
21 #include "config.h"
22 #include <stdio.h>
24 #include "tree.h"
25 #include "function.h"
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.
31 In C, this is int. */
33 tree sizetype;
35 /* An integer constant with value 0 whose type is sizetype. */
37 tree size_zero_node;
39 /* An integer constant with value 1 whose type is sizetype. */
41 tree size_one_node;
43 /* If nonzero, this is an upper limit on alignment of structure fields.
44 The value is measured in bits. */
45 int maximum_field_alignment;
47 #define GET_MODE_ALIGNMENT(MODE) \
48 MIN (BIGGEST_ALIGNMENT, \
49 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
51 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
52 enum mode_class));
53 static tree layout_record PROTO((tree));
54 static void layout_union PROTO((tree));
56 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
58 static tree pending_sizes;
60 /* Nonzero means cannot safely call expand_expr now,
61 so put variable sizes onto `pending_sizes' instead. */
63 int immediate_size_expand;
65 tree
66 get_pending_sizes ()
68 tree chain = pending_sizes;
69 tree t;
71 /* Put each SAVE_EXPR into the current function. */
72 for (t = chain; t; t = TREE_CHAIN (t))
73 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
74 pending_sizes = 0;
75 return chain;
78 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
79 to serve as the actual size-expression for a type or decl. */
81 tree
82 variable_size (size)
83 tree size;
85 /* If the language-processor is to take responsibility for variable-sized
86 items (e.g., languages which have elaboration procedures like Ada),
87 just return SIZE unchanged. */
88 if (global_bindings_p () < 0)
89 return size;
91 size = save_expr (size);
93 if (global_bindings_p ())
95 if (TREE_CONSTANT (size))
96 error ("type size can't be explicitly evaluated");
97 else
98 error ("variable-size type declared outside of any function");
100 return size_int (1);
103 if (immediate_size_expand)
104 /* NULL_RTX is not defined; neither is the rtx type.
105 Also, we would like to pass const0_rtx here, but don't have it. */
106 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
107 VOIDmode, 0);
108 else
109 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
111 return size;
114 #ifndef MAX_FIXED_MODE_SIZE
115 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
116 #endif
118 /* Return the machine mode to use for a nonscalar of SIZE bits.
119 The mode must be in class CLASS, and have exactly that many bits.
120 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
121 be used. */
123 enum machine_mode
124 mode_for_size (size, class, limit)
125 unsigned int size;
126 enum mode_class class;
127 int limit;
129 register enum machine_mode mode;
131 if (limit && size > MAX_FIXED_MODE_SIZE)
132 return BLKmode;
134 /* Get the first mode which has this size, in the specified class. */
135 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
136 mode = GET_MODE_WIDER_MODE (mode))
137 if (GET_MODE_BITSIZE (mode) == size)
138 return mode;
140 return BLKmode;
143 /* Similar, but never return BLKmode; return the narrowest mode that
144 contains at least the requested number of bits. */
146 static enum machine_mode
147 smallest_mode_for_size (size, class)
148 unsigned int size;
149 enum mode_class class;
151 register enum machine_mode mode;
153 /* Get the first mode which has at least this size, in the
154 specified class. */
155 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
156 mode = GET_MODE_WIDER_MODE (mode))
157 if (GET_MODE_BITSIZE (mode) >= size)
158 return mode;
160 abort ();
163 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
165 tree
166 round_up (value, divisor)
167 tree value;
168 int divisor;
170 return size_binop (MULT_EXPR,
171 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
172 size_int (divisor));
175 /* Set the size, mode and alignment of a ..._DECL node.
176 TYPE_DECL does need this for C++.
177 Note that LABEL_DECL and CONST_DECL nodes do not need this,
178 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
179 Don't call layout_decl for them.
181 KNOWN_ALIGN is the amount of alignment we can assume this
182 decl has with no special effort. It is relevant only for FIELD_DECLs
183 and depends on the previous fields.
184 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
185 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
186 the record will be aligned to suit. */
188 void
189 layout_decl (decl, known_align)
190 tree decl;
191 unsigned known_align;
193 register tree type = TREE_TYPE (decl);
194 register enum tree_code code = TREE_CODE (decl);
195 int spec_size = DECL_FIELD_SIZE (decl);
197 if (code == CONST_DECL)
198 return;
200 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
201 && code != FIELD_DECL && code != TYPE_DECL)
202 abort ();
204 if (type == error_mark_node)
206 type = void_type_node;
207 spec_size = 0;
210 /* Usually the size and mode come from the data type without change. */
212 DECL_MODE (decl) = TYPE_MODE (type);
213 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
214 if (DECL_SIZE (decl) == 0)
215 DECL_SIZE (decl) = TYPE_SIZE (type);
217 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
219 /* This is a bit-field. We don't know how to handle
220 them except for integral types, and front ends should
221 never generate them otherwise. */
223 if (! INTEGRAL_TYPE_P (type))
224 abort ();
226 if (spec_size == 0 && DECL_NAME (decl) != 0)
227 abort ();
229 /* Size is specified number of bits. */
230 DECL_SIZE (decl) = size_int (spec_size);
232 /* Force alignment required for the data type.
233 But if the decl itself wants greater alignment, don't override that.
234 Likewise, if the decl is packed, don't override it. */
235 else if (DECL_ALIGN (decl) == 0
236 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
237 DECL_ALIGN (decl) = TYPE_ALIGN (type);
239 /* See if we can use an ordinary integer mode for a bit-field. */
240 /* Conditions are: a fixed size that is correct for another mode
241 and occupying a complete byte or bytes on proper boundary. */
242 if (code == FIELD_DECL)
244 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
245 if (maximum_field_alignment != 0)
246 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
249 if (DECL_BIT_FIELD (decl)
250 && TYPE_SIZE (type) != 0
251 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
253 register enum machine_mode xmode
254 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
256 if (xmode != BLKmode
257 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
259 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
260 DECL_ALIGN (decl));
261 DECL_MODE (decl) = xmode;
262 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
263 /* This no longer needs to be accessed as a bit field. */
264 DECL_BIT_FIELD (decl) = 0;
268 /* Evaluate nonconstant size only once, either now or as soon as safe. */
269 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
270 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
273 /* Lay out a RECORD_TYPE type (a C struct).
274 This means laying out the fields, determining their positions,
275 and computing the overall size and required alignment of the record.
276 Note that if you set the TYPE_ALIGN before calling this
277 then the struct is aligned to at least that boundary.
279 If the type has basetypes, you must call layout_basetypes
280 before calling this function.
282 The return value is a list of static members of the record.
283 They still need to be laid out. */
285 static tree
286 layout_record (rec)
287 tree rec;
289 register tree field;
290 #ifdef STRUCTURE_SIZE_BOUNDARY
291 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
292 #else
293 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
294 #endif
295 /* These must be laid out *after* the record is. */
296 tree pending_statics = NULL_TREE;
297 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
298 where CONST_SIZE is an integer
299 and VAR_SIZE is a tree expression.
300 If VAR_SIZE is null, the size is just CONST_SIZE.
301 Naturally we try to avoid using VAR_SIZE. */
302 register int const_size = 0;
303 register tree var_size = 0;
304 /* Once we start using VAR_SIZE, this is the maximum alignment
305 that we know VAR_SIZE has. */
306 register int var_align = BITS_PER_UNIT;
309 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
311 register int known_align = var_size ? var_align : const_size;
312 register int desired_align;
314 /* If FIELD is static, then treat it like a separate variable,
315 not really like a structure field.
316 If it is a FUNCTION_DECL, it's a method.
317 In both cases, all we do is lay out the decl,
318 and we do it *after* the record is laid out. */
320 if (TREE_STATIC (field))
322 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
323 continue;
325 /* Enumerators and enum types which are local to this class need not
326 be laid out. Likewise for initialized constant fields. */
327 if (TREE_CODE (field) != FIELD_DECL)
328 continue;
330 /* Lay out the field so we know what alignment it needs.
331 For a packed field, use the alignment as specified,
332 disregarding what the type would want. */
333 if (DECL_PACKED (field))
334 desired_align = DECL_ALIGN (field);
335 layout_decl (field, known_align);
336 if (! DECL_PACKED (field))
337 desired_align = DECL_ALIGN (field);
338 /* Some targets (i.e. VMS) limit struct field alignment
339 to a lower boundary than alignment of variables. */
340 #ifdef BIGGEST_FIELD_ALIGNMENT
341 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
342 #endif
344 /* Record must have at least as much alignment as any field.
345 Otherwise, the alignment of the field within the record
346 is meaningless. */
348 #ifndef PCC_BITFIELD_TYPE_MATTERS
349 record_align = MAX (record_align, desired_align);
350 #else
351 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
352 && DECL_BIT_FIELD_TYPE (field)
353 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
355 /* For these machines, a zero-length field does not
356 affect the alignment of the structure as a whole.
357 It does, however, affect the alignment of the next field
358 within the structure. */
359 if (! integer_zerop (DECL_SIZE (field)))
360 record_align = MAX (record_align, desired_align);
361 else if (! DECL_PACKED (field))
362 desired_align = TYPE_ALIGN (TREE_TYPE (field));
363 /* A named bit field of declared type `int'
364 forces the entire structure to have `int' alignment. */
365 if (DECL_NAME (field) != 0)
367 int type_align = TYPE_ALIGN (TREE_TYPE (field));
368 if (maximum_field_alignment != 0)
369 type_align = MIN (type_align, maximum_field_alignment);
371 record_align = MAX (record_align, type_align);
374 else
375 record_align = MAX (record_align, desired_align);
376 #endif
378 /* Does this field automatically have alignment it needs
379 by virtue of the fields that precede it and the record's
380 own alignment? */
382 if (const_size % desired_align != 0
383 || (var_align % desired_align != 0
384 && var_size != 0))
386 /* No, we need to skip space before this field.
387 Bump the cumulative size to multiple of field alignment. */
389 if (var_size == 0
390 || var_align % desired_align == 0)
391 const_size
392 = CEIL (const_size, desired_align) * desired_align;
393 else
395 if (const_size > 0)
396 var_size = size_binop (PLUS_EXPR, var_size,
397 size_int (const_size));
398 const_size = 0;
399 var_size = round_up (var_size, desired_align);
400 var_align = MIN (var_align, desired_align);
404 #ifdef PCC_BITFIELD_TYPE_MATTERS
405 if (PCC_BITFIELD_TYPE_MATTERS
406 && TREE_CODE (field) == FIELD_DECL
407 && TREE_TYPE (field) != error_mark_node
408 && DECL_BIT_FIELD_TYPE (field)
409 && !DECL_PACKED (field)
410 /* If #pragma pack is in effect, turn off this feature. */
411 && maximum_field_alignment == 0
412 && !integer_zerop (DECL_SIZE (field)))
414 int type_align = TYPE_ALIGN (TREE_TYPE (field));
415 register tree dsize = DECL_SIZE (field);
416 int field_size = TREE_INT_CST_LOW (dsize);
418 /* A bit field may not span the unit of alignment of its type.
419 Advance to next boundary if necessary. */
420 /* ??? There is some uncertainty here as to what
421 should be done if type_align is less than the width of the type.
422 That can happen because the width exceeds BIGGEST_ALIGNMENT
423 or because it exceeds maximum_field_alignment. */
424 if (const_size / type_align
425 != (const_size + field_size - 1) / type_align)
426 const_size = CEIL (const_size, type_align) * type_align;
428 #endif
430 /* No existing machine description uses this parameter.
431 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
432 #ifdef BITFIELD_NBYTES_LIMITED
433 if (BITFIELD_NBYTES_LIMITED
434 && TREE_CODE (field) == FIELD_DECL
435 && TREE_TYPE (field) != error_mark_node
436 && DECL_BIT_FIELD_TYPE (field)
437 && !DECL_PACKED (field)
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 if (maximum_field_alignment != 0)
445 type_align = MIN (type_align, maximum_field_alignment);
447 /* A bit field may not span the unit of alignment of its type.
448 Advance to next boundary if necessary. */
449 if (const_size / type_align
450 != (const_size + field_size - 1) / type_align)
451 const_size = CEIL (const_size, type_align) * type_align;
453 #endif
455 /* Size so far becomes the position of this field. */
457 if (var_size && const_size)
458 DECL_FIELD_BITPOS (field)
459 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
460 else if (var_size)
461 DECL_FIELD_BITPOS (field) = var_size;
462 else
464 DECL_FIELD_BITPOS (field) = size_int (const_size);
466 /* If this field ended up more aligned than we thought it
467 would be (we approximate this by seeing if its position
468 changed), lay out the field again; perhaps we can use an
469 integral mode for it now. */
470 if (known_align != const_size)
471 layout_decl (field, const_size);
474 /* Now add size of this field to the size of the record. */
477 register tree dsize = DECL_SIZE (field);
479 /* This can happen when we have an invalid nested struct definition,
480 such as struct j { struct j { int i; } }. The error message is
481 printed in finish_struct. */
482 if (dsize == 0)
483 /* Do nothing. */;
484 else if (TREE_CODE (dsize) == INTEGER_CST
485 && TREE_INT_CST_HIGH (dsize) == 0
486 && TREE_INT_CST_LOW (dsize) + const_size > const_size)
487 /* Use const_size if there's no overflow. */
488 const_size += TREE_INT_CST_LOW (dsize);
489 else
491 if (var_size == 0)
492 var_size = dsize;
493 else
494 var_size = size_binop (PLUS_EXPR, var_size, dsize);
499 /* Work out the total size and alignment of the record
500 as one expression and store in the record type.
501 Round it up to a multiple of the record's alignment. */
503 if (var_size == 0)
505 TYPE_SIZE (rec) = size_int (const_size);
507 else
509 if (const_size)
510 var_size
511 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
512 TYPE_SIZE (rec) = var_size;
515 /* Determine the desired alignment. */
516 #ifdef ROUND_TYPE_ALIGN
517 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
518 #else
519 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
520 #endif
522 #ifdef ROUND_TYPE_SIZE
523 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
524 #else
525 /* Round the size up to be a multiple of the required alignment */
526 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
527 #endif
529 return pending_statics;
532 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
533 Lay out all the fields, set their positions to zero,
534 and compute the size and alignment of the union (maximum of any field).
535 Note that if you set the TYPE_ALIGN before calling this
536 then the union align is aligned to at least that boundary. */
538 static void
539 layout_union (rec)
540 tree rec;
542 register tree field;
543 #ifdef STRUCTURE_SIZE_BOUNDARY
544 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
545 #else
546 unsigned union_align = BITS_PER_UNIT;
547 #endif
549 /* The size of the union, based on the fields scanned so far,
550 is max (CONST_SIZE, VAR_SIZE).
551 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
552 register int const_size = 0;
553 register tree var_size = 0;
555 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
556 the reverse order in building the COND_EXPR that denotes its
557 size. We reverse them again later. */
558 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
559 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
561 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
563 /* Enums which are local to this class need not be laid out. */
564 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
565 continue;
567 layout_decl (field, 0);
568 DECL_FIELD_BITPOS (field) = size_int (0);
570 /* Union must be at least as aligned as any field requires. */
572 union_align = MAX (union_align, DECL_ALIGN (field));
574 #ifdef PCC_BITFIELD_TYPE_MATTERS
575 /* On the m88000, a bit field of declare type `int'
576 forces the entire union to have `int' alignment. */
577 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
578 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
579 #endif
581 if (TREE_CODE (rec) == UNION_TYPE)
583 /* Set union_size to max (decl_size, union_size).
584 There are more and less general ways to do this.
585 Use only CONST_SIZE unless forced to use VAR_SIZE. */
587 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
588 const_size
589 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
590 else if (var_size == 0)
591 var_size = DECL_SIZE (field);
592 else
593 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
595 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
596 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
597 DECL_SIZE (field),
598 var_size ? var_size : integer_zero_node));
601 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
602 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
604 /* Determine the ultimate size of the union (in bytes). */
605 if (NULL == var_size)
606 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
607 * BITS_PER_UNIT);
608 else if (const_size == 0)
609 TYPE_SIZE (rec) = var_size;
610 else
611 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
612 round_up (size_int (const_size),
613 BITS_PER_UNIT));
615 /* Determine the desired alignment. */
616 #ifdef ROUND_TYPE_ALIGN
617 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
618 #else
619 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
620 #endif
622 #ifdef ROUND_TYPE_SIZE
623 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
624 #else
625 /* Round the size up to be a multiple of the required alignment */
626 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
627 #endif
630 /* Calculate the mode, size, and alignment for TYPE.
631 For an array type, calculate the element separation as well.
632 Record TYPE on the chain of permanent or temporary types
633 so that dbxout will find out about it.
635 TYPE_SIZE of a type is nonzero if the type has been laid out already.
636 layout_type does nothing on such a type.
638 If the type is incomplete, its TYPE_SIZE remains zero. */
640 void
641 layout_type (type)
642 tree type;
644 int old;
645 tree pending_statics;
647 if (type == 0)
648 abort ();
650 /* Do nothing if type has been laid out before. */
651 if (TYPE_SIZE (type))
652 return;
654 /* Make sure all nodes we allocate are not momentary;
655 they must last past the current statement. */
656 old = suspend_momentary ();
658 /* Put all our nodes into the same obstack as the type. Also,
659 make expressions saveable (this is a no-op for permanent types). */
661 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
662 saveable_allocation ();
664 switch (TREE_CODE (type))
666 case LANG_TYPE:
667 /* This kind of type is the responsibility
668 of the languge-specific code. */
669 abort ();
671 case INTEGER_TYPE:
672 case ENUMERAL_TYPE:
673 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
674 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
675 TREE_UNSIGNED (type) = 1;
677 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
678 MODE_INT);
679 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
680 break;
682 case REAL_TYPE:
683 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
684 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
685 break;
687 case COMPLEX_TYPE:
688 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
689 TYPE_MODE (type)
690 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
691 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
692 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
694 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
695 break;
697 case VOID_TYPE:
698 TYPE_SIZE (type) = size_zero_node;
699 TYPE_ALIGN (type) = 1;
700 TYPE_MODE (type) = VOIDmode;
701 break;
703 case OFFSET_TYPE:
704 TYPE_SIZE (type) = size_int (POINTER_SIZE);
705 TYPE_MODE (type) = mode_for_size (POINTER_SIZE,
706 GET_MODE_CLASS (Pmode), 0);
707 break;
709 case FUNCTION_TYPE:
710 case METHOD_TYPE:
711 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
712 TYPE_SIZE (type) = size_int (2 * POINTER_SIZE);
713 break;
715 case POINTER_TYPE:
716 case REFERENCE_TYPE:
717 TYPE_MODE (type) = mode_for_size (POINTER_SIZE,
718 GET_MODE_CLASS (Pmode), 0);
719 TYPE_SIZE (type) = size_int (POINTER_SIZE);
720 TREE_UNSIGNED (type) = 1;
721 TYPE_PRECISION (type) = POINTER_SIZE;
722 break;
724 case ARRAY_TYPE:
726 register tree index = TYPE_DOMAIN (type);
727 register tree element = TREE_TYPE (type);
729 build_pointer_type (element);
731 /* We need to know both bounds in order to compute the size. */
732 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
733 && TYPE_SIZE (element))
735 tree length
736 = size_binop (PLUS_EXPR, size_one_node,
737 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
738 TYPE_MIN_VALUE (index)));
740 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
741 TYPE_SIZE (element));
744 /* Now round the alignment and size,
745 using machine-dependent criteria if any. */
747 #ifdef ROUND_TYPE_ALIGN
748 TYPE_ALIGN (type)
749 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
750 #else
751 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
752 #endif
754 #ifdef ROUND_TYPE_SIZE
755 if (TYPE_SIZE (type) != 0)
756 TYPE_SIZE (type)
757 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
758 #endif
760 TYPE_MODE (type) = BLKmode;
761 if (TYPE_SIZE (type) != 0
762 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
763 /* BLKmode elements force BLKmode aggregate;
764 else extract/store fields may lose. */
765 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
766 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
768 TYPE_MODE (type)
769 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
770 MODE_INT, 1);
772 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
773 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
774 && TYPE_MODE (type) != BLKmode)
776 TYPE_NO_FORCE_BLK (type) = 1;
777 TYPE_MODE (type) = BLKmode;
780 break;
783 case RECORD_TYPE:
784 pending_statics = layout_record (type);
785 TYPE_MODE (type) = BLKmode;
786 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
788 tree field;
789 /* A record which has any BLKmode members must itself be BLKmode;
790 it can't go in a register.
791 Unless the member is BLKmode only because it isn't aligned. */
792 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
794 int bitpos;
796 if (TREE_CODE (field) != FIELD_DECL)
797 continue;
799 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
800 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
801 goto record_lose;
803 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
804 goto record_lose;
806 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
808 /* Must be BLKmode if any field crosses a word boundary,
809 since extract_bit_field can't handle that in registers. */
810 if (bitpos / BITS_PER_WORD
811 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
812 / BITS_PER_WORD)
813 /* But there is no problem if the field is entire words. */
814 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
815 goto record_lose;
818 TYPE_MODE (type)
819 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
820 MODE_INT, 1);
822 /* If structure's known alignment is less than
823 what the scalar mode would need, and it matters,
824 then stick with BLKmode. */
825 if (STRICT_ALIGNMENT
826 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
827 || (TYPE_ALIGN (type)
828 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
830 if (TYPE_MODE (type) != BLKmode)
831 /* If this is the only reason this type is BLKmode,
832 then don't force containing types to be BLKmode. */
833 TYPE_NO_FORCE_BLK (type) = 1;
834 TYPE_MODE (type) = BLKmode;
837 record_lose: ;
840 /* Lay out any static members. This is done now
841 because their type may use the record's type. */
842 while (pending_statics)
844 layout_decl (TREE_VALUE (pending_statics), 0);
845 pending_statics = TREE_CHAIN (pending_statics);
847 break;
849 case UNION_TYPE:
850 case QUAL_UNION_TYPE:
851 layout_union (type);
852 TYPE_MODE (type) = BLKmode;
853 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
854 /* If structure's known alignment is less than
855 what the scalar mode would need, and it matters,
856 then stick with BLKmode. */
857 && (! STRICT_ALIGNMENT
858 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
859 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
861 tree field;
862 /* A union which has any BLKmode members must itself be BLKmode;
863 it can't go in a register.
864 Unless the member is BLKmode only because it isn't aligned. */
865 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
867 if (TREE_CODE (field) != FIELD_DECL)
868 continue;
870 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
871 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
872 goto union_lose;
875 TYPE_MODE (type)
876 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
877 MODE_INT, 1);
879 union_lose: ;
881 break;
883 /* Pascal and Chill types */
884 case BOOLEAN_TYPE: /* store one byte/boolean for now. */
885 TYPE_MODE (type) = QImode;
886 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
887 TYPE_PRECISION (type) = 1;
888 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
889 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
890 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
891 TREE_UNSIGNED (type) = 1;
892 break;
894 case CHAR_TYPE:
895 TYPE_MODE (type) = QImode;
896 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
897 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
898 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
899 break;
901 case FILE_TYPE:
902 /* The size may vary in different languages, so the language front end
903 should fill in the size. */
904 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
905 TYPE_MODE (type) = BLKmode;
906 break;
908 default:
909 abort ();
910 } /* end switch */
912 /* Normally, use the alignment corresponding to the mode chosen.
913 However, where strict alignment is not required, avoid
914 over-aligning structures, since most compilers do not do this
915 alignment. */
917 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
918 && (STRICT_ALIGNMENT
919 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
920 && TREE_CODE (type) != QUAL_UNION_TYPE
921 && TREE_CODE (type) != ARRAY_TYPE)))
922 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
924 /* Evaluate nonconstant size only once, either now or as soon as safe. */
925 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
926 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
928 /* Also layout any other variants of the type. */
929 if (TYPE_NEXT_VARIANT (type)
930 || type != TYPE_MAIN_VARIANT (type))
932 tree variant;
933 /* Record layout info of this variant. */
934 tree size = TYPE_SIZE (type);
935 int align = TYPE_ALIGN (type);
936 enum machine_mode mode = TYPE_MODE (type);
938 /* Copy it into all variants. */
939 for (variant = TYPE_MAIN_VARIANT (type);
940 variant;
941 variant = TYPE_NEXT_VARIANT (variant))
943 TYPE_SIZE (variant) = size;
944 TYPE_ALIGN (variant) = align;
945 TYPE_MODE (variant) = mode;
949 pop_obstacks ();
950 resume_momentary (old);
953 /* Create and return a type for signed integers of PRECISION bits. */
955 tree
956 make_signed_type (precision)
957 int precision;
959 register tree type = make_node (INTEGER_TYPE);
961 TYPE_PRECISION (type) = precision;
963 /* Create the extreme values based on the number of bits. */
965 TYPE_MIN_VALUE (type)
966 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
967 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
968 (((HOST_WIDE_INT) (-1)
969 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
970 ? precision - HOST_BITS_PER_WIDE_INT - 1
971 : 0))));
972 TYPE_MAX_VALUE (type)
973 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
974 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
975 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
976 ? (((HOST_WIDE_INT) 1
977 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
978 : 0));
980 /* Give this type's extreme values this type as their type. */
982 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
983 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
985 /* The first type made with this or `make_unsigned_type'
986 is the type for size values. */
988 if (sizetype == 0)
990 sizetype = type;
993 /* Lay out the type: set its alignment, size, etc. */
995 layout_type (type);
997 return type;
1000 /* Create and return a type for unsigned integers of PRECISION bits. */
1002 tree
1003 make_unsigned_type (precision)
1004 int precision;
1006 register tree type = make_node (INTEGER_TYPE);
1008 TYPE_PRECISION (type) = precision;
1010 /* The first type made with this or `make_signed_type'
1011 is the type for size values. */
1013 if (sizetype == 0)
1015 sizetype = type;
1018 fixup_unsigned_type (type);
1019 return type;
1022 /* Set the extreme values of TYPE based on its precision in bits,
1023 then lay it out. Used when make_signed_type won't do
1024 because the tree code is not INTEGER_TYPE.
1025 E.g. for Pascal, when the -fsigned-char option is given. */
1027 void
1028 fixup_signed_type (type)
1029 tree type;
1031 register int precision = TYPE_PRECISION (type);
1033 TYPE_MIN_VALUE (type)
1034 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1035 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1036 (((HOST_WIDE_INT) (-1)
1037 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1038 ? precision - HOST_BITS_PER_WIDE_INT - 1
1039 : 0))));
1040 TYPE_MAX_VALUE (type)
1041 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1042 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1043 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1044 ? (((HOST_WIDE_INT) 1
1045 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1046 : 0));
1048 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1049 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1051 /* Lay out the type: set its alignment, size, etc. */
1053 layout_type (type);
1056 /* Set the extreme values of TYPE based on its precision in bits,
1057 then lay it out. This is used both in `make_unsigned_type'
1058 and for enumeral types. */
1060 void
1061 fixup_unsigned_type (type)
1062 tree type;
1064 register int precision = TYPE_PRECISION (type);
1066 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1067 TYPE_MAX_VALUE (type)
1068 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1069 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1070 precision - HOST_BITS_PER_WIDE_INT > 0
1071 ? ((unsigned HOST_WIDE_INT) ~0
1072 >> (HOST_BITS_PER_WIDE_INT
1073 - (precision - HOST_BITS_PER_WIDE_INT)))
1074 : 0);
1075 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1076 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1078 /* Lay out the type: set its alignment, size, etc. */
1080 layout_type (type);
1083 /* Find the best machine mode to use when referencing a bit field of length
1084 BITSIZE bits starting at BITPOS.
1086 The underlying object is known to be aligned to a boundary of ALIGN bits.
1087 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1088 larger than LARGEST_MODE (usually SImode).
1090 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1091 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1092 mode meeting these conditions.
1094 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1095 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1096 all the conditions. */
1098 enum machine_mode
1099 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1100 int bitsize, bitpos;
1101 int align;
1102 enum machine_mode largest_mode;
1103 int volatilep;
1105 enum machine_mode mode;
1106 int unit;
1108 /* Find the narrowest integer mode that contains the bit field. */
1109 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1110 mode = GET_MODE_WIDER_MODE (mode))
1112 unit = GET_MODE_BITSIZE (mode);
1113 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1114 break;
1117 if (mode == MAX_MACHINE_MODE
1118 /* It is tempting to omit the following line
1119 if STRICT_ALIGNMENT is true.
1120 But that is incorrect, since if the bitfield uses part of 3 bytes
1121 and we use a 4-byte mode, we could get a spurious segv
1122 if the extra 4th byte is past the end of memory.
1123 (Though at least one Unix compiler ignores this problem:
1124 that on the Sequent 386 machine. */
1125 || MIN (unit, BIGGEST_ALIGNMENT) > align
1126 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1127 return VOIDmode;
1129 if (SLOW_BYTE_ACCESS && ! volatilep)
1131 enum machine_mode wide_mode = VOIDmode, tmode;
1133 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1134 tmode = GET_MODE_WIDER_MODE (tmode))
1136 unit = GET_MODE_BITSIZE (tmode);
1137 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1138 && unit <= BITS_PER_WORD
1139 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1140 && (largest_mode == VOIDmode
1141 || unit <= GET_MODE_BITSIZE (largest_mode)))
1142 wide_mode = tmode;
1145 if (wide_mode != VOIDmode)
1146 return wide_mode;
1149 return mode;
1152 /* Save all variables describing the current status into the structure *P.
1153 This is used before starting a nested function. */
1155 void
1156 save_storage_status (p)
1157 struct function *p;
1159 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1160 p->pending_sizes = pending_sizes;
1161 p->immediate_size_expand = immediate_size_expand;
1162 #endif /* 0 */
1165 /* Restore all variables describing the current status from the structure *P.
1166 This is used after a nested function. */
1168 void
1169 restore_storage_status (p)
1170 struct function *p;
1172 #if 0
1173 pending_sizes = p->pending_sizes;
1174 immediate_size_expand = p->immediate_size_expand;
1175 #endif /* 0 */