Initial revision
[official-gcc.git] / gcc / stor-layout.c
blob7c4b4f69c7ce93cbd57f474248e54db67c014442
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 88, 92-96, 1997 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include <stdio.h>
25 #include "tree.h"
26 #include "flags.h"
27 #include "except.h"
28 #include "function.h"
30 #define CEIL(x,y) (((x) + (y) - 1) / (y))
32 /* Data type for the expressions representing sizes of data types.
33 It is the first integer type laid out.
34 In C, this is int. */
36 tree sizetype;
38 /* An integer constant with value 0 whose type is sizetype. */
40 tree size_zero_node;
42 /* An integer constant with value 1 whose type is sizetype. */
44 tree size_one_node;
46 /* If nonzero, this is an upper limit on alignment of structure fields.
47 The value is measured in bits. */
48 int maximum_field_alignment;
50 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
51 May be overridden by front-ends. */
52 int set_alignment = 0;
54 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
55 enum mode_class));
56 static tree layout_record PROTO((tree));
57 static void layout_union PROTO((tree));
59 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
61 static tree pending_sizes;
63 /* Nonzero means cannot safely call expand_expr now,
64 so put variable sizes onto `pending_sizes' instead. */
66 int immediate_size_expand;
68 tree
69 get_pending_sizes ()
71 tree chain = pending_sizes;
72 tree t;
74 /* Put each SAVE_EXPR into the current function. */
75 for (t = chain; t; t = TREE_CHAIN (t))
76 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
77 pending_sizes = 0;
78 return chain;
81 void
82 put_pending_sizes (chain)
83 tree chain;
85 if (pending_sizes)
86 abort ();
88 pending_sizes = chain;
91 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
92 to serve as the actual size-expression for a type or decl. */
94 tree
95 variable_size (size)
96 tree size;
98 /* If the language-processor is to take responsibility for variable-sized
99 items (e.g., languages which have elaboration procedures like Ada),
100 just return SIZE unchanged. Likewise for self-referential sizes. */
101 if (TREE_CONSTANT (size)
102 || global_bindings_p () < 0 || contains_placeholder_p (size))
103 return size;
105 size = save_expr (size);
107 if (global_bindings_p ())
109 if (TREE_CONSTANT (size))
110 error ("type size can't be explicitly evaluated");
111 else
112 error ("variable-size type declared outside of any function");
114 return size_int (1);
117 if (immediate_size_expand)
118 /* NULL_RTX is not defined; neither is the rtx type.
119 Also, we would like to pass const0_rtx here, but don't have it. */
120 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
121 VOIDmode, 0);
122 else
123 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
125 return size;
128 #ifndef MAX_FIXED_MODE_SIZE
129 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
130 #endif
132 /* Return the machine mode to use for a nonscalar of SIZE bits.
133 The mode must be in class CLASS, and have exactly that many bits.
134 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
135 be used. */
137 enum machine_mode
138 mode_for_size (size, class, limit)
139 unsigned int size;
140 enum mode_class class;
141 int limit;
143 register enum machine_mode mode;
145 if (limit && size > MAX_FIXED_MODE_SIZE)
146 return BLKmode;
148 /* Get the first mode which has this size, in the specified class. */
149 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
150 mode = GET_MODE_WIDER_MODE (mode))
151 if (GET_MODE_BITSIZE (mode) == size)
152 return mode;
154 return BLKmode;
157 /* Similar, but never return BLKmode; return the narrowest mode that
158 contains at least the requested number of bits. */
160 static enum machine_mode
161 smallest_mode_for_size (size, class)
162 unsigned int size;
163 enum mode_class class;
165 register enum machine_mode mode;
167 /* Get the first mode which has at least this size, in the
168 specified class. */
169 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
170 mode = GET_MODE_WIDER_MODE (mode))
171 if (GET_MODE_BITSIZE (mode) >= size)
172 return mode;
174 abort ();
177 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
179 tree
180 round_up (value, divisor)
181 tree value;
182 int divisor;
184 return size_binop (MULT_EXPR,
185 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
186 size_int (divisor));
189 /* Set the size, mode and alignment of a ..._DECL node.
190 TYPE_DECL does need this for C++.
191 Note that LABEL_DECL and CONST_DECL nodes do not need this,
192 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
193 Don't call layout_decl for them.
195 KNOWN_ALIGN is the amount of alignment we can assume this
196 decl has with no special effort. It is relevant only for FIELD_DECLs
197 and depends on the previous fields.
198 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
199 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
200 the record will be aligned to suit. */
202 void
203 layout_decl (decl, known_align)
204 tree decl;
205 unsigned known_align;
207 register tree type = TREE_TYPE (decl);
208 register enum tree_code code = TREE_CODE (decl);
209 int spec_size = DECL_FIELD_SIZE (decl);
211 if (code == CONST_DECL)
212 return;
214 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
215 && code != FIELD_DECL && code != TYPE_DECL)
216 abort ();
218 if (type == error_mark_node)
220 type = void_type_node;
221 spec_size = 0;
224 /* Usually the size and mode come from the data type without change. */
226 DECL_MODE (decl) = TYPE_MODE (type);
227 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
228 if (DECL_SIZE (decl) == 0)
229 DECL_SIZE (decl) = TYPE_SIZE (type);
231 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
233 if (spec_size == 0 && DECL_NAME (decl) != 0)
234 abort ();
236 /* Size is specified number of bits. */
237 DECL_SIZE (decl) = size_int (spec_size);
239 /* Force alignment required for the data type.
240 But if the decl itself wants greater alignment, don't override that.
241 Likewise, if the decl is packed, don't override it. */
242 else if (DECL_ALIGN (decl) == 0
243 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
244 DECL_ALIGN (decl) = TYPE_ALIGN (type);
246 /* See if we can use an ordinary integer mode for a bit-field. */
247 /* Conditions are: a fixed size that is correct for another mode
248 and occupying a complete byte or bytes on proper boundary. */
249 if (code == FIELD_DECL)
251 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
252 if (maximum_field_alignment != 0)
253 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
254 else if (DECL_PACKED (decl))
255 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
258 if (DECL_BIT_FIELD (decl)
259 && TYPE_SIZE (type) != 0
260 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
261 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
263 register enum machine_mode xmode
264 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
266 if (xmode != BLKmode
267 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
269 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
270 DECL_ALIGN (decl));
271 DECL_MODE (decl) = xmode;
272 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
273 /* This no longer needs to be accessed as a bit field. */
274 DECL_BIT_FIELD (decl) = 0;
278 /* Turn off DECL_BIT_FIELD if we won't need it set. */
279 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
280 && known_align % TYPE_ALIGN (type) == 0
281 && DECL_SIZE (decl) != 0
282 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
283 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
284 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
285 DECL_BIT_FIELD (decl) = 0;
287 /* Evaluate nonconstant size only once, either now or as soon as safe. */
288 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
289 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
292 /* Lay out a RECORD_TYPE type (a C struct).
293 This means laying out the fields, determining their positions,
294 and computing the overall size and required alignment of the record.
295 Note that if you set the TYPE_ALIGN before calling this
296 then the struct is aligned to at least that boundary.
298 If the type has basetypes, you must call layout_basetypes
299 before calling this function.
301 The return value is a list of static members of the record.
302 They still need to be laid out. */
304 static tree
305 layout_record (rec)
306 tree rec;
308 register tree field;
309 #ifdef STRUCTURE_SIZE_BOUNDARY
310 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
311 #else
312 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
313 #endif
314 /* These must be laid out *after* the record is. */
315 tree pending_statics = NULL_TREE;
316 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
317 where CONST_SIZE is an integer
318 and VAR_SIZE is a tree expression.
319 If VAR_SIZE is null, the size is just CONST_SIZE.
320 Naturally we try to avoid using VAR_SIZE. */
321 register HOST_WIDE_INT const_size = 0;
322 register tree var_size = 0;
323 /* Once we start using VAR_SIZE, this is the maximum alignment
324 that we know VAR_SIZE has. */
325 register int var_align = BITS_PER_UNIT;
328 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
330 register int known_align = var_size ? var_align : const_size;
331 register int desired_align;
333 /* If FIELD is static, then treat it like a separate variable,
334 not really like a structure field.
335 If it is a FUNCTION_DECL, it's a method.
336 In both cases, all we do is lay out the decl,
337 and we do it *after* the record is laid out. */
339 if (TREE_CODE (field) == VAR_DECL)
341 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
342 continue;
344 /* Enumerators and enum types which are local to this class need not
345 be laid out. Likewise for initialized constant fields. */
346 if (TREE_CODE (field) != FIELD_DECL)
347 continue;
349 /* Lay out the field so we know what alignment it needs.
350 For a packed field, use the alignment as specified,
351 disregarding what the type would want. */
352 if (DECL_PACKED (field))
353 desired_align = DECL_ALIGN (field);
354 layout_decl (field, known_align);
355 if (! DECL_PACKED (field))
356 desired_align = DECL_ALIGN (field);
357 /* Some targets (i.e. VMS) limit struct field alignment
358 to a lower boundary than alignment of variables. */
359 #ifdef BIGGEST_FIELD_ALIGNMENT
360 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
361 #endif
362 #ifdef ADJUST_FIELD_ALIGN
363 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
364 #endif
366 /* Record must have at least as much alignment as any field.
367 Otherwise, the alignment of the field within the record
368 is meaningless. */
370 #ifndef PCC_BITFIELD_TYPE_MATTERS
371 record_align = MAX (record_align, desired_align);
372 #else
373 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
374 && DECL_BIT_FIELD_TYPE (field)
375 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
377 /* For these machines, a zero-length field does not
378 affect the alignment of the structure as a whole.
379 It does, however, affect the alignment of the next field
380 within the structure. */
381 if (! integer_zerop (DECL_SIZE (field)))
382 record_align = MAX (record_align, desired_align);
383 else if (! DECL_PACKED (field))
384 desired_align = TYPE_ALIGN (TREE_TYPE (field));
385 /* A named bit field of declared type `int'
386 forces the entire structure to have `int' alignment. */
387 if (DECL_NAME (field) != 0)
389 int type_align = TYPE_ALIGN (TREE_TYPE (field));
390 if (maximum_field_alignment != 0)
391 type_align = MIN (type_align, maximum_field_alignment);
392 else if (TYPE_PACKED (rec))
393 type_align = MIN (type_align, BITS_PER_UNIT);
395 record_align = MAX (record_align, type_align);
398 else
399 record_align = MAX (record_align, desired_align);
400 #endif
402 /* Does this field automatically have alignment it needs
403 by virtue of the fields that precede it and the record's
404 own alignment? */
406 if (const_size % desired_align != 0
407 || (var_align % desired_align != 0
408 && var_size != 0))
410 /* No, we need to skip space before this field.
411 Bump the cumulative size to multiple of field alignment. */
413 if (var_size == 0
414 || var_align % desired_align == 0)
415 const_size
416 = CEIL (const_size, desired_align) * desired_align;
417 else
419 if (const_size > 0)
420 var_size = size_binop (PLUS_EXPR, var_size,
421 size_int (const_size));
422 const_size = 0;
423 var_size = round_up (var_size, desired_align);
424 var_align = MIN (var_align, desired_align);
428 #ifdef PCC_BITFIELD_TYPE_MATTERS
429 if (PCC_BITFIELD_TYPE_MATTERS
430 && TREE_CODE (field) == FIELD_DECL
431 && TREE_TYPE (field) != error_mark_node
432 && DECL_BIT_FIELD_TYPE (field)
433 && !DECL_PACKED (field)
434 && maximum_field_alignment == 0
435 && !integer_zerop (DECL_SIZE (field)))
437 int type_align = TYPE_ALIGN (TREE_TYPE (field));
438 register tree dsize = DECL_SIZE (field);
439 int field_size = TREE_INT_CST_LOW (dsize);
441 /* A bit field may not span more units of alignment of its type
442 than its type itself. Advance to next boundary if necessary. */
443 if (((const_size + field_size + type_align - 1) / type_align
444 - const_size / type_align)
445 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
446 const_size = CEIL (const_size, type_align) * type_align;
448 #endif
450 /* No existing machine description uses this parameter.
451 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
452 #ifdef BITFIELD_NBYTES_LIMITED
453 if (BITFIELD_NBYTES_LIMITED
454 && TREE_CODE (field) == FIELD_DECL
455 && TREE_TYPE (field) != error_mark_node
456 && DECL_BIT_FIELD_TYPE (field)
457 && !DECL_PACKED (field)
458 && !integer_zerop (DECL_SIZE (field)))
460 int type_align = TYPE_ALIGN (TREE_TYPE (field));
461 register tree dsize = DECL_SIZE (field);
462 int field_size = TREE_INT_CST_LOW (dsize);
464 if (maximum_field_alignment != 0)
465 type_align = MIN (type_align, maximum_field_alignment);
466 else if (TYPE_PACKED (rec))
467 type_align = MIN (type_align, BITS_PER_UNIT);
469 /* A bit field may not span the unit of alignment of its type.
470 Advance to next boundary if necessary. */
471 if (const_size / type_align
472 != (const_size + field_size - 1) / type_align)
473 const_size = CEIL (const_size, type_align) * type_align;
475 #endif
477 /* Size so far becomes the position of this field. */
479 if (var_size && const_size)
480 DECL_FIELD_BITPOS (field)
481 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
482 else if (var_size)
483 DECL_FIELD_BITPOS (field) = var_size;
484 else
486 DECL_FIELD_BITPOS (field) = size_int (const_size);
488 /* If this field ended up more aligned than we thought it
489 would be (we approximate this by seeing if its position
490 changed), lay out the field again; perhaps we can use an
491 integral mode for it now. */
492 if (known_align != const_size)
493 layout_decl (field, const_size);
496 /* Now add size of this field to the size of the record. */
499 register tree dsize = DECL_SIZE (field);
501 /* This can happen when we have an invalid nested struct definition,
502 such as struct j { struct j { int i; } }. The error message is
503 printed in finish_struct. */
504 if (dsize == 0)
505 /* Do nothing. */;
506 else if (TREE_CODE (dsize) == INTEGER_CST
507 && ! TREE_CONSTANT_OVERFLOW (dsize)
508 && TREE_INT_CST_HIGH (dsize) == 0
509 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
510 /* Use const_size if there's no overflow. */
511 const_size += TREE_INT_CST_LOW (dsize);
512 else
514 if (var_size == 0)
515 var_size = dsize;
516 else
517 var_size = size_binop (PLUS_EXPR, var_size, dsize);
522 /* Work out the total size and alignment of the record
523 as one expression and store in the record type.
524 Round it up to a multiple of the record's alignment. */
526 if (var_size == 0)
528 TYPE_SIZE (rec) = size_int (const_size);
530 else
532 if (const_size)
533 var_size
534 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
535 TYPE_SIZE (rec) = var_size;
538 /* Determine the desired alignment. */
539 #ifdef ROUND_TYPE_ALIGN
540 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
541 #else
542 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
543 #endif
545 #ifdef ROUND_TYPE_SIZE
546 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
547 #else
548 /* Round the size up to be a multiple of the required alignment */
549 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
550 #endif
552 return pending_statics;
555 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
556 Lay out all the fields, set their positions to zero,
557 and compute the size and alignment of the union (maximum of any field).
558 Note that if you set the TYPE_ALIGN before calling this
559 then the union align is aligned to at least that boundary. */
561 static void
562 layout_union (rec)
563 tree rec;
565 register tree field;
566 #ifdef STRUCTURE_SIZE_BOUNDARY
567 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
568 #else
569 unsigned union_align = BITS_PER_UNIT;
570 #endif
572 /* The size of the union, based on the fields scanned so far,
573 is max (CONST_SIZE, VAR_SIZE).
574 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
575 register int const_size = 0;
576 register tree var_size = 0;
578 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
579 the reverse order in building the COND_EXPR that denotes its
580 size. We reverse them again later. */
581 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
582 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
584 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
586 /* Enums which are local to this class need not be laid out. */
587 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
588 continue;
590 layout_decl (field, 0);
591 DECL_FIELD_BITPOS (field) = size_int (0);
593 /* Union must be at least as aligned as any field requires. */
595 union_align = MAX (union_align, DECL_ALIGN (field));
597 #ifdef PCC_BITFIELD_TYPE_MATTERS
598 /* On the m88000, a bit field of declare type `int'
599 forces the entire union to have `int' alignment. */
600 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
601 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
602 #endif
604 if (TREE_CODE (rec) == UNION_TYPE)
606 /* Set union_size to max (decl_size, union_size).
607 There are more and less general ways to do this.
608 Use only CONST_SIZE unless forced to use VAR_SIZE. */
610 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
611 const_size
612 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
613 else if (var_size == 0)
614 var_size = DECL_SIZE (field);
615 else
616 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
618 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
619 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
620 DECL_SIZE (field),
621 var_size ? var_size : integer_zero_node));
624 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
625 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
627 /* Determine the ultimate size of the union (in bytes). */
628 if (NULL == var_size)
629 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
630 * BITS_PER_UNIT);
631 else if (const_size == 0)
632 TYPE_SIZE (rec) = var_size;
633 else
634 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
635 round_up (size_int (const_size),
636 BITS_PER_UNIT));
638 /* Determine the desired alignment. */
639 #ifdef ROUND_TYPE_ALIGN
640 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
641 #else
642 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
643 #endif
645 #ifdef ROUND_TYPE_SIZE
646 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
647 #else
648 /* Round the size up to be a multiple of the required alignment */
649 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
650 #endif
653 /* Calculate the mode, size, and alignment for TYPE.
654 For an array type, calculate the element separation as well.
655 Record TYPE on the chain of permanent or temporary types
656 so that dbxout will find out about it.
658 TYPE_SIZE of a type is nonzero if the type has been laid out already.
659 layout_type does nothing on such a type.
661 If the type is incomplete, its TYPE_SIZE remains zero. */
663 void
664 layout_type (type)
665 tree type;
667 int old;
668 tree pending_statics;
670 if (type == 0)
671 abort ();
673 /* Do nothing if type has been laid out before. */
674 if (TYPE_SIZE (type))
675 return;
677 /* Make sure all nodes we allocate are not momentary;
678 they must last past the current statement. */
679 old = suspend_momentary ();
681 /* Put all our nodes into the same obstack as the type. Also,
682 make expressions saveable (this is a no-op for permanent types). */
684 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
685 saveable_allocation ();
687 switch (TREE_CODE (type))
689 case LANG_TYPE:
690 /* This kind of type is the responsibility
691 of the language-specific code. */
692 abort ();
694 case INTEGER_TYPE:
695 case ENUMERAL_TYPE:
696 case CHAR_TYPE:
697 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
698 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
699 TREE_UNSIGNED (type) = 1;
701 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
702 MODE_INT);
703 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
704 break;
706 case REAL_TYPE:
707 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
708 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
709 break;
711 case COMPLEX_TYPE:
712 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
713 TYPE_MODE (type)
714 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
715 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
716 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
718 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
719 break;
721 case VOID_TYPE:
722 TYPE_SIZE (type) = size_zero_node;
723 TYPE_ALIGN (type) = 1;
724 TYPE_MODE (type) = VOIDmode;
725 break;
727 case OFFSET_TYPE:
728 TYPE_SIZE (type) = size_int (POINTER_SIZE);
729 TYPE_MODE (type) = ptr_mode;
730 break;
732 case FUNCTION_TYPE:
733 case METHOD_TYPE:
734 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
735 TYPE_SIZE (type) = size_int (2 * POINTER_SIZE);
736 break;
738 case POINTER_TYPE:
739 case REFERENCE_TYPE:
740 TYPE_MODE (type) = ptr_mode;
741 TYPE_SIZE (type) = size_int (POINTER_SIZE);
742 TREE_UNSIGNED (type) = 1;
743 TYPE_PRECISION (type) = POINTER_SIZE;
744 break;
746 case ARRAY_TYPE:
748 register tree index = TYPE_DOMAIN (type);
749 register tree element = TREE_TYPE (type);
751 build_pointer_type (element);
753 /* We need to know both bounds in order to compute the size. */
754 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
755 && TYPE_SIZE (element))
757 tree ub = TYPE_MAX_VALUE (index);
758 tree lb = TYPE_MIN_VALUE (index);
759 tree length;
761 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
762 test for negative below covers it. */
763 if (TREE_CODE (ub) == MAX_EXPR
764 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
765 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
766 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
767 lb, 0))
768 ub = TREE_OPERAND (ub, 1);
769 else if (TREE_CODE (ub) == MAX_EXPR
770 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
771 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
772 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
774 lb, 0))
775 ub = TREE_OPERAND (ub, 0);
777 length = size_binop (PLUS_EXPR, size_one_node,
778 size_binop (MINUS_EXPR, ub, lb));
780 /* If neither bound is a constant and sizetype is signed, make
781 sure the size is never negative. We should really do this
782 if *either* bound is non-constant, but this is the best
783 compromise between C and Ada. */
784 if (! TREE_UNSIGNED (sizetype)
785 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
786 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
787 length = size_binop (MAX_EXPR, length, size_zero_node);
789 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
790 TYPE_SIZE (element));
793 /* Now round the alignment and size,
794 using machine-dependent criteria if any. */
796 #ifdef ROUND_TYPE_ALIGN
797 TYPE_ALIGN (type)
798 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
799 #else
800 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
801 #endif
803 #ifdef ROUND_TYPE_SIZE
804 if (TYPE_SIZE (type) != 0)
805 TYPE_SIZE (type)
806 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
807 #endif
809 TYPE_MODE (type) = BLKmode;
810 if (TYPE_SIZE (type) != 0
811 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
812 /* BLKmode elements force BLKmode aggregate;
813 else extract/store fields may lose. */
814 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
815 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
817 TYPE_MODE (type)
818 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
819 MODE_INT, 1);
821 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
822 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
823 && TYPE_MODE (type) != BLKmode)
825 TYPE_NO_FORCE_BLK (type) = 1;
826 TYPE_MODE (type) = BLKmode;
829 break;
832 case RECORD_TYPE:
833 pending_statics = layout_record (type);
834 TYPE_MODE (type) = BLKmode;
835 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
837 tree field;
838 enum machine_mode mode = VOIDmode;
840 /* A record which has any BLKmode members must itself be BLKmode;
841 it can't go in a register.
842 Unless the member is BLKmode only because it isn't aligned. */
843 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
845 int bitpos;
847 if (TREE_CODE (field) != FIELD_DECL)
848 continue;
850 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
851 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
852 goto record_lose;
854 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
855 goto record_lose;
857 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
859 /* Must be BLKmode if any field crosses a word boundary,
860 since extract_bit_field can't handle that in registers. */
861 if (bitpos / BITS_PER_WORD
862 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
863 / BITS_PER_WORD)
864 /* But there is no problem if the field is entire words. */
865 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
866 goto record_lose;
868 /* If this field is the whole struct, remember its mode so
869 that, say, we can put a double in a class into a DF
870 register instead of forcing it to live in the stack. */
871 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
872 mode = DECL_MODE (field);
875 if (mode != VOIDmode)
876 /* We only have one real field; use its mode. */
877 TYPE_MODE (type) = mode;
878 else
879 TYPE_MODE (type)
880 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
881 MODE_INT, 1);
883 /* If structure's known alignment is less than
884 what the scalar mode would need, and it matters,
885 then stick with BLKmode. */
886 if (STRICT_ALIGNMENT
887 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
888 || (TYPE_ALIGN (type)
889 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
891 if (TYPE_MODE (type) != BLKmode)
892 /* If this is the only reason this type is BLKmode,
893 then don't force containing types to be BLKmode. */
894 TYPE_NO_FORCE_BLK (type) = 1;
895 TYPE_MODE (type) = BLKmode;
898 record_lose: ;
901 /* Lay out any static members. This is done now
902 because their type may use the record's type. */
903 while (pending_statics)
905 layout_decl (TREE_VALUE (pending_statics), 0);
906 pending_statics = TREE_CHAIN (pending_statics);
908 break;
910 case UNION_TYPE:
911 case QUAL_UNION_TYPE:
912 layout_union (type);
913 TYPE_MODE (type) = BLKmode;
914 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
915 /* If structure's known alignment is less than
916 what the scalar mode would need, and it matters,
917 then stick with BLKmode. */
918 && (! STRICT_ALIGNMENT
919 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
920 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
922 tree field;
923 /* A union which has any BLKmode members must itself be BLKmode;
924 it can't go in a register.
925 Unless the member is BLKmode only because it isn't aligned. */
926 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
928 if (TREE_CODE (field) != FIELD_DECL)
929 continue;
931 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
932 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
933 goto union_lose;
936 TYPE_MODE (type)
937 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
938 MODE_INT, 1);
940 union_lose: ;
942 break;
944 /* Pascal and Chill types */
945 case BOOLEAN_TYPE: /* store one byte/boolean for now. */
946 TYPE_MODE (type) = QImode;
947 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
948 TYPE_PRECISION (type) = 1;
949 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
950 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
951 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
952 TREE_UNSIGNED (type) = 1;
953 break;
955 case SET_TYPE:
956 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
957 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
958 abort();
959 else
961 #ifndef SET_WORD_SIZE
962 #define SET_WORD_SIZE BITS_PER_WORD
963 #endif
964 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
965 int size_in_bits
966 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
967 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
968 int rounded_size
969 = ((size_in_bits + alignment - 1) / alignment) * alignment;
970 if (rounded_size > alignment)
971 TYPE_MODE (type) = BLKmode;
972 else
973 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
974 TYPE_SIZE (type) = size_int (rounded_size);
975 TYPE_ALIGN (type) = alignment;
976 TYPE_PRECISION (type) = size_in_bits;
978 break;
980 case FILE_TYPE:
981 /* The size may vary in different languages, so the language front end
982 should fill in the size. */
983 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
984 TYPE_MODE (type) = BLKmode;
985 break;
987 default:
988 abort ();
989 } /* end switch */
991 /* Normally, use the alignment corresponding to the mode chosen.
992 However, where strict alignment is not required, avoid
993 over-aligning structures, since most compilers do not do this
994 alignment. */
996 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
997 && (STRICT_ALIGNMENT
998 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
999 && TREE_CODE (type) != QUAL_UNION_TYPE
1000 && TREE_CODE (type) != ARRAY_TYPE)))
1001 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1003 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1004 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1005 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1007 /* Also layout any other variants of the type. */
1008 if (TYPE_NEXT_VARIANT (type)
1009 || type != TYPE_MAIN_VARIANT (type))
1011 tree variant;
1012 /* Record layout info of this variant. */
1013 tree size = TYPE_SIZE (type);
1014 int align = TYPE_ALIGN (type);
1015 enum machine_mode mode = TYPE_MODE (type);
1017 /* Copy it into all variants. */
1018 for (variant = TYPE_MAIN_VARIANT (type);
1019 variant;
1020 variant = TYPE_NEXT_VARIANT (variant))
1022 TYPE_SIZE (variant) = size;
1023 TYPE_ALIGN (variant) = align;
1024 TYPE_MODE (variant) = mode;
1028 pop_obstacks ();
1029 resume_momentary (old);
1032 /* Create and return a type for signed integers of PRECISION bits. */
1034 tree
1035 make_signed_type (precision)
1036 int precision;
1038 register tree type = make_node (INTEGER_TYPE);
1040 TYPE_PRECISION (type) = precision;
1042 /* Create the extreme values based on the number of bits. */
1044 TYPE_MIN_VALUE (type)
1045 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1046 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1047 (((HOST_WIDE_INT) (-1)
1048 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1049 ? precision - HOST_BITS_PER_WIDE_INT - 1
1050 : 0))));
1051 TYPE_MAX_VALUE (type)
1052 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1053 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1054 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1055 ? (((HOST_WIDE_INT) 1
1056 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1057 : 0));
1059 /* Give this type's extreme values this type as their type. */
1061 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1062 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1064 /* The first type made with this or `make_unsigned_type'
1065 is the type for size values. */
1067 if (sizetype == 0)
1069 sizetype = type;
1072 /* Lay out the type: set its alignment, size, etc. */
1074 layout_type (type);
1076 return type;
1079 /* Create and return a type for unsigned integers of PRECISION bits. */
1081 tree
1082 make_unsigned_type (precision)
1083 int precision;
1085 register tree type = make_node (INTEGER_TYPE);
1087 TYPE_PRECISION (type) = precision;
1089 /* The first type made with this or `make_signed_type'
1090 is the type for size values. */
1092 if (sizetype == 0)
1094 sizetype = type;
1097 fixup_unsigned_type (type);
1098 return type;
1101 /* Set the extreme values of TYPE based on its precision in bits,
1102 then lay it out. Used when make_signed_type won't do
1103 because the tree code is not INTEGER_TYPE.
1104 E.g. for Pascal, when the -fsigned-char option is given. */
1106 void
1107 fixup_signed_type (type)
1108 tree type;
1110 register int precision = TYPE_PRECISION (type);
1112 TYPE_MIN_VALUE (type)
1113 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1114 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1115 (((HOST_WIDE_INT) (-1)
1116 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1117 ? precision - HOST_BITS_PER_WIDE_INT - 1
1118 : 0))));
1119 TYPE_MAX_VALUE (type)
1120 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1121 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1122 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1123 ? (((HOST_WIDE_INT) 1
1124 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1125 : 0));
1127 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1128 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1130 /* Lay out the type: set its alignment, size, etc. */
1132 layout_type (type);
1135 /* Set the extreme values of TYPE based on its precision in bits,
1136 then lay it out. This is used both in `make_unsigned_type'
1137 and for enumeral types. */
1139 void
1140 fixup_unsigned_type (type)
1141 tree type;
1143 register int precision = TYPE_PRECISION (type);
1145 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1146 TYPE_MAX_VALUE (type)
1147 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1148 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1149 precision - HOST_BITS_PER_WIDE_INT > 0
1150 ? ((unsigned HOST_WIDE_INT) ~0
1151 >> (HOST_BITS_PER_WIDE_INT
1152 - (precision - HOST_BITS_PER_WIDE_INT)))
1153 : 0);
1154 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1155 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1157 /* Lay out the type: set its alignment, size, etc. */
1159 layout_type (type);
1162 /* Find the best machine mode to use when referencing a bit field of length
1163 BITSIZE bits starting at BITPOS.
1165 The underlying object is known to be aligned to a boundary of ALIGN bits.
1166 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1167 larger than LARGEST_MODE (usually SImode).
1169 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1170 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1171 mode meeting these conditions.
1173 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1174 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1175 all the conditions. */
1177 enum machine_mode
1178 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1179 int bitsize, bitpos;
1180 int align;
1181 enum machine_mode largest_mode;
1182 int volatilep;
1184 enum machine_mode mode;
1185 int unit;
1187 /* Find the narrowest integer mode that contains the bit field. */
1188 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1189 mode = GET_MODE_WIDER_MODE (mode))
1191 unit = GET_MODE_BITSIZE (mode);
1192 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1193 break;
1196 if (mode == MAX_MACHINE_MODE
1197 /* It is tempting to omit the following line
1198 if STRICT_ALIGNMENT is true.
1199 But that is incorrect, since if the bitfield uses part of 3 bytes
1200 and we use a 4-byte mode, we could get a spurious segv
1201 if the extra 4th byte is past the end of memory.
1202 (Though at least one Unix compiler ignores this problem:
1203 that on the Sequent 386 machine. */
1204 || MIN (unit, BIGGEST_ALIGNMENT) > align
1205 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1206 return VOIDmode;
1208 if (SLOW_BYTE_ACCESS && ! volatilep)
1210 enum machine_mode wide_mode = VOIDmode, tmode;
1212 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1213 tmode = GET_MODE_WIDER_MODE (tmode))
1215 unit = GET_MODE_BITSIZE (tmode);
1216 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1217 && unit <= BITS_PER_WORD
1218 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1219 && (largest_mode == VOIDmode
1220 || unit <= GET_MODE_BITSIZE (largest_mode)))
1221 wide_mode = tmode;
1224 if (wide_mode != VOIDmode)
1225 return wide_mode;
1228 return mode;
1231 /* Save all variables describing the current status into the structure *P.
1232 This is used before starting a nested function. */
1234 void
1235 save_storage_status (p)
1236 struct function *p;
1238 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1239 p->pending_sizes = pending_sizes;
1240 p->immediate_size_expand = immediate_size_expand;
1241 #endif /* 0 */
1244 /* Restore all variables describing the current status from the structure *P.
1245 This is used after a nested function. */
1247 void
1248 restore_storage_status (p)
1249 struct function *p;
1251 #if 0
1252 pending_sizes = p->pending_sizes;
1253 immediate_size_expand = p->immediate_size_expand;
1254 #endif /* 0 */