Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / stor-layout.c
blob4ac26a20912002d5ae69d420342fec99c48aa5bd
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)
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 "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "except.h"
27 #include "function.h"
29 #define CEIL(x,y) (((x) + (y) - 1) / (y))
31 /* Data type for the expressions representing sizes of data types.
32 It is the first integer type laid out.
33 In C, this is int. */
35 tree sizetype;
37 /* An integer constant with value 0 whose type is sizetype. */
39 tree size_zero_node;
41 /* An integer constant with value 1 whose type is sizetype. */
43 tree size_one_node;
45 /* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
47 int maximum_field_alignment;
49 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
50 May be overridden by front-ends. */
51 int set_alignment = 0;
53 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
54 enum mode_class));
55 static tree layout_record PROTO((tree));
56 static void layout_union PROTO((tree));
58 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
60 static tree pending_sizes;
62 /* Nonzero means cannot safely call expand_expr now,
63 so put variable sizes onto `pending_sizes' instead. */
65 int immediate_size_expand;
67 tree
68 get_pending_sizes ()
70 tree chain = pending_sizes;
71 tree t;
73 /* Put each SAVE_EXPR into the current function. */
74 for (t = chain; t; t = TREE_CHAIN (t))
75 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
76 pending_sizes = 0;
77 return chain;
80 void
81 put_pending_sizes (chain)
82 tree chain;
84 if (pending_sizes)
85 abort ();
87 pending_sizes = chain;
90 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
91 to serve as the actual size-expression for a type or decl. */
93 tree
94 variable_size (size)
95 tree size;
97 /* If the language-processor is to take responsibility for variable-sized
98 items (e.g., languages which have elaboration procedures like Ada),
99 just return SIZE unchanged. Likewise for self-referential sizes. */
100 if (TREE_CONSTANT (size)
101 || global_bindings_p () < 0 || contains_placeholder_p (size))
102 return size;
104 size = save_expr (size);
106 if (global_bindings_p ())
108 if (TREE_CONSTANT (size))
109 error ("type size can't be explicitly evaluated");
110 else
111 error ("variable-size type declared outside of any function");
113 return size_int (1);
116 if (immediate_size_expand)
117 /* NULL_RTX is not defined; neither is the rtx type.
118 Also, we would like to pass const0_rtx here, but don't have it. */
119 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
120 VOIDmode, 0);
121 else
122 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
124 return size;
127 #ifndef MAX_FIXED_MODE_SIZE
128 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
129 #endif
131 /* Return the machine mode to use for a nonscalar of SIZE bits.
132 The mode must be in class CLASS, and have exactly that many bits.
133 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
134 be used. */
136 enum machine_mode
137 mode_for_size (size, class, limit)
138 unsigned int size;
139 enum mode_class class;
140 int limit;
142 register enum machine_mode mode;
144 if (limit && size > MAX_FIXED_MODE_SIZE)
145 return BLKmode;
147 /* Get the first mode which has this size, in the specified class. */
148 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
149 mode = GET_MODE_WIDER_MODE (mode))
150 if (GET_MODE_BITSIZE (mode) == size)
151 return mode;
153 return BLKmode;
156 /* Similar, but never return BLKmode; return the narrowest mode that
157 contains at least the requested number of bits. */
159 static enum machine_mode
160 smallest_mode_for_size (size, class)
161 unsigned int size;
162 enum mode_class class;
164 register enum machine_mode mode;
166 /* Get the first mode which has at least this size, in the
167 specified class. */
168 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
169 mode = GET_MODE_WIDER_MODE (mode))
170 if (GET_MODE_BITSIZE (mode) >= size)
171 return mode;
173 abort ();
176 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
178 tree
179 round_up (value, divisor)
180 tree value;
181 int divisor;
183 return size_binop (MULT_EXPR,
184 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
185 size_int (divisor));
188 /* Set the size, mode and alignment of a ..._DECL node.
189 TYPE_DECL does need this for C++.
190 Note that LABEL_DECL and CONST_DECL nodes do not need this,
191 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
192 Don't call layout_decl for them.
194 KNOWN_ALIGN is the amount of alignment we can assume this
195 decl has with no special effort. It is relevant only for FIELD_DECLs
196 and depends on the previous fields.
197 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
198 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
199 the record will be aligned to suit. */
201 void
202 layout_decl (decl, known_align)
203 tree decl;
204 unsigned known_align;
206 register tree type = TREE_TYPE (decl);
207 register enum tree_code code = TREE_CODE (decl);
208 int spec_size = DECL_FIELD_SIZE (decl);
210 if (code == CONST_DECL)
211 return;
213 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
214 && code != FIELD_DECL && code != TYPE_DECL)
215 abort ();
217 if (type == error_mark_node)
219 type = void_type_node;
220 spec_size = 0;
223 /* Usually the size and mode come from the data type without change. */
225 DECL_MODE (decl) = TYPE_MODE (type);
226 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
227 if (DECL_SIZE (decl) == 0)
228 DECL_SIZE (decl) = TYPE_SIZE (type);
230 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
232 if (spec_size == 0 && DECL_NAME (decl) != 0)
233 abort ();
235 /* Size is specified number of bits. */
236 DECL_SIZE (decl) = size_int (spec_size);
238 /* Force alignment required for the data type.
239 But if the decl itself wants greater alignment, don't override that.
240 Likewise, if the decl is packed, don't override it. */
241 else if (DECL_ALIGN (decl) == 0
242 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
243 DECL_ALIGN (decl) = TYPE_ALIGN (type);
245 /* See if we can use an ordinary integer mode for a bit-field. */
246 /* Conditions are: a fixed size that is correct for another mode
247 and occupying a complete byte or bytes on proper boundary. */
248 if (code == FIELD_DECL)
250 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
251 if (maximum_field_alignment != 0)
252 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
253 else if (DECL_PACKED (decl))
254 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
257 if (DECL_BIT_FIELD (decl)
258 && TYPE_SIZE (type) != 0
259 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
260 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
262 register enum machine_mode xmode
263 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
265 if (xmode != BLKmode
266 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
268 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
269 DECL_ALIGN (decl));
270 DECL_MODE (decl) = xmode;
271 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
272 /* This no longer needs to be accessed as a bit field. */
273 DECL_BIT_FIELD (decl) = 0;
277 /* Turn off DECL_BIT_FIELD if we won't need it set. */
278 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
279 && known_align % TYPE_ALIGN (type) == 0
280 && DECL_SIZE (decl) != 0
281 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
282 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
283 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
284 DECL_BIT_FIELD (decl) = 0;
286 /* Evaluate nonconstant size only once, either now or as soon as safe. */
287 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
288 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
291 /* Lay out a RECORD_TYPE type (a C struct).
292 This means laying out the fields, determining their positions,
293 and computing the overall size and required alignment of the record.
294 Note that if you set the TYPE_ALIGN before calling this
295 then the struct is aligned to at least that boundary.
297 If the type has basetypes, you must call layout_basetypes
298 before calling this function.
300 The return value is a list of static members of the record.
301 They still need to be laid out. */
303 static tree
304 layout_record (rec)
305 tree rec;
307 register tree field;
308 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
309 /* These must be laid out *after* the record is. */
310 tree pending_statics = NULL_TREE;
311 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
312 where CONST_SIZE is an integer
313 and VAR_SIZE is a tree expression.
314 If VAR_SIZE is null, the size is just CONST_SIZE.
315 Naturally we try to avoid using VAR_SIZE. */
316 register HOST_WIDE_INT const_size = 0;
317 register tree var_size = 0;
318 /* Once we start using VAR_SIZE, this is the maximum alignment
319 that we know VAR_SIZE has. */
320 register int var_align = BITS_PER_UNIT;
322 #ifdef STRUCTURE_SIZE_BOUNDARY
323 /* Packed structures don't need to have minimum size. */
324 if (! TYPE_PACKED (rec))
325 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
326 #endif
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 (DECL_PACKED (field))
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 /* ??? This test is opposite the test in the containing if
467 statement, so this code is unreachable currently. */
468 else if (DECL_PACKED (field))
469 type_align = MIN (type_align, BITS_PER_UNIT);
471 /* A bit field may not span the unit of alignment of its type.
472 Advance to next boundary if necessary. */
473 /* ??? This code should match the code above for the
474 PCC_BITFIELD_TYPE_MATTERS case. */
475 if (const_size / type_align
476 != (const_size + field_size - 1) / type_align)
477 const_size = CEIL (const_size, type_align) * type_align;
479 #endif
481 /* Size so far becomes the position of this field. */
483 if (var_size && const_size)
484 DECL_FIELD_BITPOS (field)
485 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
486 else if (var_size)
487 DECL_FIELD_BITPOS (field) = var_size;
488 else
490 DECL_FIELD_BITPOS (field) = size_int (const_size);
492 /* If this field ended up more aligned than we thought it
493 would be (we approximate this by seeing if its position
494 changed), lay out the field again; perhaps we can use an
495 integral mode for it now. */
496 if (known_align != const_size)
497 layout_decl (field, const_size);
500 /* Now add size of this field to the size of the record. */
503 register tree dsize = DECL_SIZE (field);
505 /* This can happen when we have an invalid nested struct definition,
506 such as struct j { struct j { int i; } }. The error message is
507 printed in finish_struct. */
508 if (dsize == 0)
509 /* Do nothing. */;
510 else if (TREE_CODE (dsize) == INTEGER_CST
511 && ! TREE_CONSTANT_OVERFLOW (dsize)
512 && TREE_INT_CST_HIGH (dsize) == 0
513 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
514 /* Use const_size if there's no overflow. */
515 const_size += TREE_INT_CST_LOW (dsize);
516 else
518 if (var_size == 0)
519 var_size = dsize;
520 else
521 var_size = size_binop (PLUS_EXPR, var_size, dsize);
526 /* Work out the total size and alignment of the record
527 as one expression and store in the record type.
528 Round it up to a multiple of the record's alignment. */
530 if (var_size == 0)
532 TYPE_SIZE (rec) = size_int (const_size);
534 else
536 if (const_size)
537 var_size
538 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
539 TYPE_SIZE (rec) = var_size;
542 /* Determine the desired alignment. */
543 #ifdef ROUND_TYPE_ALIGN
544 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
545 #else
546 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
547 #endif
549 #ifdef ROUND_TYPE_SIZE
550 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
551 #else
552 /* Round the size up to be a multiple of the required alignment */
553 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
554 #endif
556 return pending_statics;
559 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
560 Lay out all the fields, set their positions to zero,
561 and compute the size and alignment of the union (maximum of any field).
562 Note that if you set the TYPE_ALIGN before calling this
563 then the union align is aligned to at least that boundary. */
565 static void
566 layout_union (rec)
567 tree rec;
569 register tree field;
570 unsigned union_align = BITS_PER_UNIT;
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 #ifdef STRUCTURE_SIZE_BOUNDARY
579 /* Packed structures don't need to have minimum size. */
580 if (! TYPE_PACKED (rec))
581 union_align = STRUCTURE_SIZE_BOUNDARY;
582 #endif
584 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
585 the reverse order in building the COND_EXPR that denotes its
586 size. We reverse them again later. */
587 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
588 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
590 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
592 /* Enums which are local to this class need not be laid out. */
593 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
594 continue;
596 layout_decl (field, 0);
597 DECL_FIELD_BITPOS (field) = size_int (0);
599 /* Union must be at least as aligned as any field requires. */
601 union_align = MAX (union_align, DECL_ALIGN (field));
603 #ifdef PCC_BITFIELD_TYPE_MATTERS
604 /* On the m88000, a bit field of declare type `int'
605 forces the entire union to have `int' alignment. */
606 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
607 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
608 #endif
610 if (TREE_CODE (rec) == UNION_TYPE)
612 /* Set union_size to max (decl_size, union_size).
613 There are more and less general ways to do this.
614 Use only CONST_SIZE unless forced to use VAR_SIZE. */
616 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
617 const_size
618 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
619 else if (var_size == 0)
620 var_size = DECL_SIZE (field);
621 else
622 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
624 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
625 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
626 DECL_SIZE (field),
627 var_size ? var_size : integer_zero_node));
630 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
631 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
633 /* Determine the ultimate size of the union (in bytes). */
634 if (NULL == var_size)
635 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
636 * BITS_PER_UNIT);
637 else if (const_size == 0)
638 TYPE_SIZE (rec) = var_size;
639 else
640 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
641 round_up (size_int (const_size),
642 BITS_PER_UNIT));
644 /* Determine the desired alignment. */
645 #ifdef ROUND_TYPE_ALIGN
646 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
647 #else
648 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
649 #endif
651 #ifdef ROUND_TYPE_SIZE
652 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
653 #else
654 /* Round the size up to be a multiple of the required alignment */
655 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
656 #endif
659 /* Calculate the mode, size, and alignment for TYPE.
660 For an array type, calculate the element separation as well.
661 Record TYPE on the chain of permanent or temporary types
662 so that dbxout will find out about it.
664 TYPE_SIZE of a type is nonzero if the type has been laid out already.
665 layout_type does nothing on such a type.
667 If the type is incomplete, its TYPE_SIZE remains zero. */
669 void
670 layout_type (type)
671 tree type;
673 int old;
674 tree pending_statics;
676 if (type == 0)
677 abort ();
679 /* Do nothing if type has been laid out before. */
680 if (TYPE_SIZE (type))
681 return;
683 /* Make sure all nodes we allocate are not momentary;
684 they must last past the current statement. */
685 old = suspend_momentary ();
687 /* Put all our nodes into the same obstack as the type. Also,
688 make expressions saveable (this is a no-op for permanent types). */
690 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
691 saveable_allocation ();
693 switch (TREE_CODE (type))
695 case LANG_TYPE:
696 /* This kind of type is the responsibility
697 of the language-specific code. */
698 abort ();
700 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
701 if (TYPE_PRECISION (type) == 0)
702 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
703 /* ... fall through ... */
705 case INTEGER_TYPE:
706 case ENUMERAL_TYPE:
707 case CHAR_TYPE:
708 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
709 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
710 TREE_UNSIGNED (type) = 1;
712 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
713 MODE_INT);
714 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
715 break;
717 case REAL_TYPE:
718 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
719 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
720 break;
722 case COMPLEX_TYPE:
723 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
724 TYPE_MODE (type)
725 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
726 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
727 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
729 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
730 break;
732 case VOID_TYPE:
733 TYPE_SIZE (type) = size_zero_node;
734 TYPE_ALIGN (type) = 1;
735 TYPE_MODE (type) = VOIDmode;
736 break;
738 case OFFSET_TYPE:
739 TYPE_SIZE (type) = size_int (POINTER_SIZE);
740 TYPE_MODE (type) = ptr_mode;
741 break;
743 case FUNCTION_TYPE:
744 case METHOD_TYPE:
745 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
746 TYPE_SIZE (type) = size_int (2 * POINTER_SIZE);
747 break;
749 case POINTER_TYPE:
750 case REFERENCE_TYPE:
751 TYPE_MODE (type) = ptr_mode;
752 TYPE_SIZE (type) = size_int (POINTER_SIZE);
753 TREE_UNSIGNED (type) = 1;
754 TYPE_PRECISION (type) = POINTER_SIZE;
755 break;
757 case ARRAY_TYPE:
759 register tree index = TYPE_DOMAIN (type);
760 register tree element = TREE_TYPE (type);
762 build_pointer_type (element);
764 /* We need to know both bounds in order to compute the size. */
765 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
766 && TYPE_SIZE (element))
768 tree ub = TYPE_MAX_VALUE (index);
769 tree lb = TYPE_MIN_VALUE (index);
770 tree length;
772 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
773 test for negative below covers it. */
774 if (TREE_CODE (ub) == MAX_EXPR
775 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
776 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
777 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
778 lb, 0))
779 ub = TREE_OPERAND (ub, 1);
780 else if (TREE_CODE (ub) == MAX_EXPR
781 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
782 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
783 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
785 lb, 0))
786 ub = TREE_OPERAND (ub, 0);
788 length = size_binop (PLUS_EXPR, size_one_node,
789 size_binop (MINUS_EXPR, ub, lb));
791 /* If neither bound is a constant and sizetype is signed, make
792 sure the size is never negative. We should really do this
793 if *either* bound is non-constant, but this is the best
794 compromise between C and Ada. */
795 if (! TREE_UNSIGNED (sizetype)
796 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
797 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
798 length = size_binop (MAX_EXPR, length, size_zero_node);
800 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
801 TYPE_SIZE (element));
804 /* Now round the alignment and size,
805 using machine-dependent criteria if any. */
807 #ifdef ROUND_TYPE_ALIGN
808 TYPE_ALIGN (type)
809 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
810 #else
811 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
812 #endif
814 #ifdef ROUND_TYPE_SIZE
815 if (TYPE_SIZE (type) != 0)
816 TYPE_SIZE (type)
817 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
818 #endif
820 TYPE_MODE (type) = BLKmode;
821 if (TYPE_SIZE (type) != 0
822 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
823 /* BLKmode elements force BLKmode aggregate;
824 else extract/store fields may lose. */
825 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
826 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
828 TYPE_MODE (type)
829 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
830 MODE_INT, 1);
832 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
833 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
834 && TYPE_MODE (type) != BLKmode)
836 TYPE_NO_FORCE_BLK (type) = 1;
837 TYPE_MODE (type) = BLKmode;
840 break;
843 case RECORD_TYPE:
844 pending_statics = layout_record (type);
845 TYPE_MODE (type) = BLKmode;
846 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
848 tree field;
849 enum machine_mode mode = VOIDmode;
851 /* A record which has any BLKmode members must itself be BLKmode;
852 it can't go in a register.
853 Unless the member is BLKmode only because it isn't aligned. */
854 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
856 int bitpos;
858 if (TREE_CODE (field) != FIELD_DECL)
859 continue;
861 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
862 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
863 goto record_lose;
865 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
866 goto record_lose;
868 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
870 /* Must be BLKmode if any field crosses a word boundary,
871 since extract_bit_field can't handle that in registers. */
872 if (bitpos / BITS_PER_WORD
873 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
874 / BITS_PER_WORD)
875 /* But there is no problem if the field is entire words. */
876 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
877 goto record_lose;
879 /* If this field is the whole struct, remember its mode so
880 that, say, we can put a double in a class into a DF
881 register instead of forcing it to live in the stack. */
882 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
883 mode = DECL_MODE (field);
886 if (mode != VOIDmode)
887 /* We only have one real field; use its mode. */
888 TYPE_MODE (type) = mode;
889 else
890 TYPE_MODE (type)
891 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
892 MODE_INT, 1);
894 /* If structure's known alignment is less than
895 what the scalar mode would need, and it matters,
896 then stick with BLKmode. */
897 if (STRICT_ALIGNMENT
898 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
899 || (TYPE_ALIGN (type)
900 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
902 if (TYPE_MODE (type) != BLKmode)
903 /* If this is the only reason this type is BLKmode,
904 then don't force containing types to be BLKmode. */
905 TYPE_NO_FORCE_BLK (type) = 1;
906 TYPE_MODE (type) = BLKmode;
909 record_lose: ;
912 /* Lay out any static members. This is done now
913 because their type may use the record's type. */
914 while (pending_statics)
916 layout_decl (TREE_VALUE (pending_statics), 0);
917 pending_statics = TREE_CHAIN (pending_statics);
919 break;
921 case UNION_TYPE:
922 case QUAL_UNION_TYPE:
923 layout_union (type);
924 TYPE_MODE (type) = BLKmode;
925 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
926 /* If structure's known alignment is less than
927 what the scalar mode would need, and it matters,
928 then stick with BLKmode. */
929 && (! STRICT_ALIGNMENT
930 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
931 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
933 tree field;
934 /* A union which has any BLKmode members must itself be BLKmode;
935 it can't go in a register.
936 Unless the member is BLKmode only because it isn't aligned. */
937 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
939 if (TREE_CODE (field) != FIELD_DECL)
940 continue;
942 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
943 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
944 goto union_lose;
947 TYPE_MODE (type)
948 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
949 MODE_INT, 1);
951 union_lose: ;
953 break;
955 case SET_TYPE: /* Used by Chill and Pascal. */
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) + bitsize <= 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 */