oops - omitted from previous delta fixing UNIQUE_SECTION
[official-gcc.git] / gcc / stor-layout.c
blob8ffacb749a108b619ff0844c5111950d763c26b3
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 88, 92-98, 1999, 2000 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 "rtl.h"
26 #include "tm_p.h"
27 #include "flags.h"
28 #include "function.h"
29 #include "expr.h"
30 #include "toplev.h"
31 #include "ggc.h"
33 /* Data type for the expressions representing sizes of data types.
34 It is the first integer type laid out. */
36 struct sizetype_tab sizetype_tab;
38 /* If nonzero, this is an upper limit on alignment of structure fields.
39 The value is measured in bits. */
40 int maximum_field_alignment;
42 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
43 May be overridden by front-ends. */
44 int set_alignment = 0;
46 static tree layout_record PARAMS ((tree));
47 static void layout_union PARAMS ((tree));
49 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
51 static tree pending_sizes;
53 /* Nonzero means cannot safely call expand_expr now,
54 so put variable sizes onto `pending_sizes' instead. */
56 int immediate_size_expand;
58 tree
59 get_pending_sizes ()
61 tree chain = pending_sizes;
62 tree t;
64 /* Put each SAVE_EXPR into the current function. */
65 for (t = chain; t; t = TREE_CHAIN (t))
66 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
68 pending_sizes = 0;
69 return chain;
72 void
73 put_pending_sizes (chain)
74 tree chain;
76 if (pending_sizes)
77 abort ();
79 pending_sizes = chain;
82 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
83 to serve as the actual size-expression for a type or decl. */
85 tree
86 variable_size (size)
87 tree size;
89 /* If the language-processor is to take responsibility for variable-sized
90 items (e.g., languages which have elaboration procedures like Ada),
91 just return SIZE unchanged. Likewise for self-referential sizes. */
92 if (TREE_CONSTANT (size)
93 || global_bindings_p () < 0 || contains_placeholder_p (size))
94 return size;
96 size = save_expr (size);
98 /* If an array with a variable number of elements is declared, and
99 the elements require destruction, we will emit a cleanup for the
100 array. That cleanup is run both on normal exit from the block
101 and in the exception-handler for the block. Normally, when code
102 is used in both ordinary code and in an exception handler it is
103 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
104 not wish to do that here; the array-size is the same in both
105 places. */
106 if (TREE_CODE (size) == SAVE_EXPR)
107 SAVE_EXPR_PERSISTENT_P (size) = 1;
109 if (global_bindings_p ())
111 if (TREE_CONSTANT (size))
112 error ("type size can't be explicitly evaluated");
113 else
114 error ("variable-size type declared outside of any function");
116 return size_int (1);
119 if (immediate_size_expand)
120 /* NULL_RTX is not defined; neither is the rtx type.
121 Also, we would like to pass const0_rtx here, but don't have it. */
122 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
123 VOIDmode, 0);
124 else if (cfun != 0
125 && cfun->x_dont_save_pending_sizes_p)
126 /* The front-end doesn't want us to keep a list of the expressions
127 that determine sizes for variable size objects. */
129 else
130 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
132 return size;
135 #ifndef MAX_FIXED_MODE_SIZE
136 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
137 #endif
139 /* Return the machine mode to use for a nonscalar of SIZE bits.
140 The mode must be in class CLASS, and have exactly that many bits.
141 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
142 be used. */
144 enum machine_mode
145 mode_for_size (size, class, limit)
146 unsigned int size;
147 enum mode_class class;
148 int limit;
150 register enum machine_mode mode;
152 if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
153 return BLKmode;
155 /* Get the first mode which has this size, in the specified class. */
156 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
157 mode = GET_MODE_WIDER_MODE (mode))
158 if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
159 return mode;
161 return BLKmode;
164 /* Similar, but never return BLKmode; return the narrowest mode that
165 contains at least the requested number of bits. */
167 enum machine_mode
168 smallest_mode_for_size (size, class)
169 unsigned int size;
170 enum mode_class class;
172 register enum machine_mode mode;
174 /* Get the first mode which has at least this size, in the
175 specified class. */
176 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
177 mode = GET_MODE_WIDER_MODE (mode))
178 if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
179 return mode;
181 abort ();
184 /* Find an integer mode of the exact same size, or BLKmode on failure. */
186 enum machine_mode
187 int_mode_for_mode (mode)
188 enum machine_mode mode;
190 switch (GET_MODE_CLASS (mode))
192 case MODE_INT:
193 case MODE_PARTIAL_INT:
194 break;
196 case MODE_COMPLEX_INT:
197 case MODE_COMPLEX_FLOAT:
198 case MODE_FLOAT:
199 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
200 break;
202 case MODE_RANDOM:
203 if (mode == BLKmode)
204 break;
206 /* ... fall through ... */
208 case MODE_CC:
209 default:
210 abort();
213 return mode;
216 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
218 tree
219 round_up (value, divisor)
220 tree value;
221 int divisor;
223 return size_binop (MULT_EXPR,
224 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
225 size_int (divisor));
228 /* Set the size, mode and alignment of a ..._DECL node.
229 TYPE_DECL does need this for C++.
230 Note that LABEL_DECL and CONST_DECL nodes do not need this,
231 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
232 Don't call layout_decl for them.
234 KNOWN_ALIGN is the amount of alignment we can assume this
235 decl has with no special effort. It is relevant only for FIELD_DECLs
236 and depends on the previous fields.
237 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
238 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
239 the record will be aligned to suit. */
241 void
242 layout_decl (decl, known_align)
243 tree decl;
244 unsigned known_align;
246 register tree type = TREE_TYPE (decl);
247 register enum tree_code code = TREE_CODE (decl);
248 int spec_size = DECL_FIELD_SIZE (decl);
250 if (code == CONST_DECL)
251 return;
253 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
254 && code != FIELD_DECL && code != TYPE_DECL)
255 abort ();
257 if (type == error_mark_node)
259 type = void_type_node;
260 spec_size = 0;
263 /* Usually the size and mode come from the data type without change. */
265 DECL_MODE (decl) = TYPE_MODE (type);
266 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
267 if (DECL_SIZE (decl) == 0)
268 DECL_SIZE (decl) = TYPE_SIZE (type);
270 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
272 if (spec_size == 0 && DECL_NAME (decl) != 0)
273 abort ();
275 /* Size is specified in number of bits. */
276 DECL_SIZE (decl) = bitsize_int (spec_size, 0);
278 /* Force alignment required for the data type.
279 But if the decl itself wants greater alignment, don't override that.
280 Likewise, if the decl is packed, don't override it. */
281 else if (DECL_ALIGN (decl) == 0
282 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
283 DECL_ALIGN (decl) = TYPE_ALIGN (type);
285 /* See if we can use an ordinary integer mode for a bit-field.
286 Conditions are: a fixed size that is correct for another mode
287 and occupying a complete byte or bytes on proper boundary. */
288 if (code == FIELD_DECL)
290 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
291 if (maximum_field_alignment != 0)
292 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl),
293 (unsigned)maximum_field_alignment);
294 else if (DECL_PACKED (decl))
295 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
298 if (DECL_BIT_FIELD (decl)
299 && TYPE_SIZE (type) != 0
300 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
301 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
303 register enum machine_mode xmode
304 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
306 if (xmode != BLKmode
307 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
309 DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode),
310 DECL_ALIGN (decl));
311 DECL_MODE (decl) = xmode;
312 DECL_SIZE (decl) = bitsize_int (GET_MODE_BITSIZE (xmode), 0);
313 /* This no longer needs to be accessed as a bit field. */
314 DECL_BIT_FIELD (decl) = 0;
318 /* Turn off DECL_BIT_FIELD if we won't need it set. */
319 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
320 && known_align % TYPE_ALIGN (type) == 0
321 && DECL_SIZE (decl) != 0
322 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
323 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
324 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
325 DECL_BIT_FIELD (decl) = 0;
327 /* Evaluate nonconstant size only once, either now or as soon as safe. */
328 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
329 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
332 /* Lay out a RECORD_TYPE type (a C struct).
333 This means laying out the fields, determining their positions,
334 and computing the overall size and required alignment of the record.
335 Note that if you set the TYPE_ALIGN before calling this
336 then the struct is aligned to at least that boundary.
338 If the type has basetypes, you must call layout_basetypes
339 before calling this function.
341 The return value is a list of static members of the record.
342 They still need to be laid out. */
344 static tree
345 layout_record (rec)
346 tree rec;
348 register tree field;
349 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
350 unsigned unpacked_align = record_align;
351 /* These must be laid out *after* the record is. */
352 tree pending_statics = NULL_TREE;
353 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
354 where CONST_SIZE is an integer
355 and VAR_SIZE is a tree expression.
356 If VAR_SIZE is null, the size is just CONST_SIZE.
357 Naturally we try to avoid using VAR_SIZE. */
358 register HOST_WIDE_INT const_size = 0;
359 register tree var_size = 0;
360 /* Once we start using VAR_SIZE, this is the maximum alignment
361 that we know VAR_SIZE has. */
362 register int var_align = BITS_PER_UNIT;
363 int packed_maybe_necessary = 0;
365 #ifdef STRUCTURE_SIZE_BOUNDARY
366 /* Packed structures don't need to have minimum size. */
367 if (! TYPE_PACKED (rec))
368 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
369 #endif
371 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
373 register int known_align = var_size ? var_align : const_size;
374 register int desired_align = 0;
375 tree type = TREE_TYPE (field);
377 /* If FIELD is static, then treat it like a separate variable,
378 not really like a structure field.
379 If it is a FUNCTION_DECL, it's a method.
380 In both cases, all we do is lay out the decl,
381 and we do it *after* the record is laid out. */
383 if (TREE_CODE (field) == VAR_DECL)
385 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
386 continue;
389 /* Enumerators and enum types which are local to this class need not
390 be laid out. Likewise for initialized constant fields. */
391 if (TREE_CODE (field) != FIELD_DECL)
392 continue;
394 /* Lay out the field so we know what alignment it needs.
395 For a packed field, use the alignment as specified,
396 disregarding what the type would want. */
397 if (DECL_PACKED (field))
398 desired_align = DECL_ALIGN (field);
399 layout_decl (field, known_align);
400 if (! DECL_PACKED (field))
401 desired_align = DECL_ALIGN (field);
402 /* Some targets (i.e. VMS) limit struct field alignment
403 to a lower boundary than alignment of variables. */
404 #ifdef BIGGEST_FIELD_ALIGNMENT
405 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
406 #endif
407 #ifdef ADJUST_FIELD_ALIGN
408 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
409 #endif
411 /* Record must have at least as much alignment as any field.
412 Otherwise, the alignment of the field within the record
413 is meaningless. */
415 #ifdef PCC_BITFIELD_TYPE_MATTERS
416 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
417 && DECL_BIT_FIELD_TYPE (field)
418 && ! integer_zerop (TYPE_SIZE (type)))
420 /* For these machines, a zero-length field does not
421 affect the alignment of the structure as a whole.
422 It does, however, affect the alignment of the next field
423 within the structure. */
424 if (! integer_zerop (DECL_SIZE (field)))
425 record_align = MAX ((int)record_align, desired_align);
426 else if (! DECL_PACKED (field))
427 desired_align = TYPE_ALIGN (type);
428 /* A named bit field of declared type `int'
429 forces the entire structure to have `int' alignment. */
430 if (DECL_NAME (field) != 0)
432 int type_align = TYPE_ALIGN (type);
433 if (maximum_field_alignment != 0)
434 type_align = MIN (type_align, maximum_field_alignment);
435 else if (DECL_PACKED (field))
436 type_align = MIN (type_align, BITS_PER_UNIT);
438 record_align = MAX ((int) record_align, type_align);
439 if (warn_packed)
440 unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
443 else
444 #endif
446 record_align = MAX ((int) record_align, desired_align);
447 if (warn_packed)
448 unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
451 if (warn_packed && DECL_PACKED (field))
453 if (const_size % TYPE_ALIGN (type) == 0
454 || (var_align % TYPE_ALIGN (type) == 0
455 && var_size != NULL_TREE))
457 if (TYPE_ALIGN (type) > desired_align)
459 if (STRICT_ALIGNMENT)
460 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
461 else
462 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
465 else
466 packed_maybe_necessary = 1;
469 /* Does this field automatically have alignment it needs
470 by virtue of the fields that precede it and the record's
471 own alignment? */
473 if (const_size % desired_align != 0
474 || (var_align % desired_align != 0
475 && var_size != NULL_TREE))
477 /* No, we need to skip space before this field.
478 Bump the cumulative size to multiple of field alignment. */
480 if (warn_padded)
481 warning_with_decl (field, "padding struct to align `%s'");
483 if (var_size == NULL_TREE
484 || var_align % desired_align == 0)
485 const_size
486 = CEIL (const_size, desired_align) * desired_align;
487 else
489 if (const_size > 0)
490 var_size = size_binop (PLUS_EXPR, var_size,
491 bitsize_int (const_size, 0L));
492 const_size = 0;
493 var_size = round_up (var_size, desired_align);
494 var_align = MIN (var_align, desired_align);
498 #ifdef PCC_BITFIELD_TYPE_MATTERS
499 if (PCC_BITFIELD_TYPE_MATTERS
500 && TREE_CODE (field) == FIELD_DECL
501 && type != error_mark_node
502 && DECL_BIT_FIELD_TYPE (field)
503 && !DECL_PACKED (field)
504 && maximum_field_alignment == 0
505 && !integer_zerop (DECL_SIZE (field)))
507 int type_align = TYPE_ALIGN (type);
508 register tree dsize = DECL_SIZE (field);
509 int field_size = TREE_INT_CST_LOW (dsize);
511 /* A bit field may not span more units of alignment of its type
512 than its type itself. Advance to next boundary if necessary. */
513 if (((const_size + field_size + type_align - 1) / type_align
514 - const_size / type_align)
515 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
516 const_size = CEIL (const_size, type_align) * type_align;
518 #endif
520 /* No existing machine description uses this parameter.
521 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
522 #ifdef BITFIELD_NBYTES_LIMITED
523 if (BITFIELD_NBYTES_LIMITED
524 && TREE_CODE (field) == FIELD_DECL
525 && type != error_mark_node
526 && DECL_BIT_FIELD_TYPE (field)
527 && !DECL_PACKED (field)
528 && !integer_zerop (DECL_SIZE (field)))
530 int type_align = TYPE_ALIGN (type);
531 register tree dsize = DECL_SIZE (field);
532 int field_size = TREE_INT_CST_LOW (dsize);
534 if (maximum_field_alignment != 0)
535 type_align = MIN (type_align, maximum_field_alignment);
536 /* ??? This test is opposite the test in the containing if
537 statement, so this code is unreachable currently. */
538 else if (DECL_PACKED (field))
539 type_align = MIN (type_align, BITS_PER_UNIT);
541 /* A bit field may not span the unit of alignment of its type.
542 Advance to next boundary if necessary. */
543 /* ??? This code should match the code above for the
544 PCC_BITFIELD_TYPE_MATTERS case. */
545 if (const_size / type_align
546 != (const_size + field_size - 1) / type_align)
547 const_size = CEIL (const_size, type_align) * type_align;
549 #endif
551 /* Size so far becomes the position of this field. */
553 if (var_size && const_size)
554 DECL_FIELD_BITPOS (field)
555 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
556 else if (var_size)
557 DECL_FIELD_BITPOS (field) = var_size;
558 else
560 DECL_FIELD_BITPOS (field) = bitsize_int (const_size, 0L);
562 /* If this field ended up more aligned than we thought it
563 would be (we approximate this by seeing if its position
564 changed), lay out the field again; perhaps we can use an
565 integral mode for it now. */
566 if (known_align != const_size)
567 layout_decl (field, const_size);
570 /* Now add size of this field to the size of the record. */
573 register tree dsize = DECL_SIZE (field);
575 /* This can happen when we have an invalid nested struct definition,
576 such as struct j { struct j { int i; } }. The error message is
577 printed in finish_struct. */
578 if (dsize == 0)
579 /* Do nothing. */;
580 else if (TREE_CODE (dsize) == INTEGER_CST
581 && ! TREE_CONSTANT_OVERFLOW (dsize)
582 && TREE_INT_CST_HIGH (dsize) == 0
583 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
584 /* Use const_size if there's no overflow. */
585 const_size += TREE_INT_CST_LOW (dsize);
586 else
588 if (var_size == NULL_TREE)
589 var_size = dsize;
590 else
591 var_size = size_binop (PLUS_EXPR, var_size, dsize);
596 /* Work out the total size and alignment of the record
597 as one expression and store in the record type.
598 Round it up to a multiple of the record's alignment. */
600 if (var_size == NULL_TREE)
602 TYPE_SIZE (rec) = bitsize_int (const_size, 0L);
604 else
606 if (const_size)
607 var_size
608 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
609 TYPE_SIZE (rec) = var_size;
612 /* Determine the desired alignment. */
613 #ifdef ROUND_TYPE_ALIGN
614 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
615 #else
616 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
617 #endif
619 /* Record the un-rounded size in the binfo node. But first we check
620 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
621 if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
622 TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
625 tree unpadded_size = TYPE_SIZE (rec);
626 #ifdef ROUND_TYPE_SIZE
627 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
628 #else
629 /* Round the size up to be a multiple of the required alignment */
630 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
631 #endif
632 if (warn_padded && var_size == NULL_TREE
633 && simple_cst_equal (unpadded_size, TYPE_SIZE (rec)) == 0)
634 warning ("padding struct size to alignment boundary");
637 if (warn_packed && TYPE_PACKED (rec) && !packed_maybe_necessary
638 && var_size == NULL_TREE)
640 tree unpacked_size;
641 TYPE_PACKED (rec) = 0;
642 #ifdef ROUND_TYPE_ALIGN
643 unpacked_align
644 = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), unpacked_align);
645 #else
646 unpacked_align = MAX (TYPE_ALIGN (rec), unpacked_align);
647 #endif
648 #ifdef ROUND_TYPE_SIZE
649 unpacked_size = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), unpacked_align);
650 #else
651 unpacked_size = round_up (TYPE_SIZE (rec), unpacked_align);
652 #endif
653 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rec)))
655 if (TYPE_NAME (rec))
657 char *name;
658 if (TREE_CODE (TYPE_NAME (rec)) == IDENTIFIER_NODE)
659 name = IDENTIFIER_POINTER (TYPE_NAME (rec));
660 else
661 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rec)));
662 if (STRICT_ALIGNMENT)
663 warning ("packed attribute causes inefficient alignment for `%s'", name);
664 else
665 warning ("packed attribute is unnecessary for `%s'", name);
667 else
669 if (STRICT_ALIGNMENT)
670 warning ("packed attribute causes inefficient alignment");
671 else
672 warning ("packed attribute is unnecessary");
675 TYPE_PACKED (rec) = 1;
678 return pending_statics;
681 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
682 Lay out all the fields, set their positions to zero,
683 and compute the size and alignment of the union (maximum of any field).
684 Note that if you set the TYPE_ALIGN before calling this
685 then the union align is aligned to at least that boundary. */
687 static void
688 layout_union (rec)
689 tree rec;
691 register tree field;
692 unsigned union_align = BITS_PER_UNIT;
694 /* The size of the union, based on the fields scanned so far,
695 is max (CONST_SIZE, VAR_SIZE).
696 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
697 register HOST_WIDE_INT const_size = 0;
698 register tree var_size = 0;
700 #ifdef STRUCTURE_SIZE_BOUNDARY
701 /* Packed structures don't need to have minimum size. */
702 if (! TYPE_PACKED (rec))
703 union_align = STRUCTURE_SIZE_BOUNDARY;
704 #endif
706 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
707 the reverse order in building the COND_EXPR that denotes its
708 size. We reverse them again later. */
709 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
710 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
712 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
714 tree dsize;
716 /* Enums which are local to this class need not be laid out. */
717 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
718 continue;
720 layout_decl (field, 0);
721 DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
723 /* Union must be at least as aligned as any field requires. */
725 union_align = MAX (union_align, DECL_ALIGN (field));
727 #ifdef PCC_BITFIELD_TYPE_MATTERS
728 /* On the m88000, a bit field of declare type `int'
729 forces the entire union to have `int' alignment. */
730 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
731 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
732 #endif
734 dsize = DECL_SIZE (field);
735 if (TREE_CODE (rec) == UNION_TYPE)
737 /* Set union_size to max (decl_size, union_size).
738 There are more and less general ways to do this.
739 Use only CONST_SIZE unless forced to use VAR_SIZE. */
741 if (TREE_CODE (dsize) == INTEGER_CST
742 && ! TREE_CONSTANT_OVERFLOW (dsize)
743 && TREE_INT_CST_HIGH (dsize) == 0)
744 const_size
745 = MAX (const_size, TREE_INT_CST_LOW (dsize));
746 else if (var_size == 0)
747 var_size = dsize;
748 else
749 var_size = size_binop (MAX_EXPR, var_size, dsize);
751 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
752 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
753 DECL_SIZE (field),
754 var_size ? var_size : bitsize_int (0L, 0L)));
757 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
758 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
760 /* Determine the ultimate size of the union (in bytes). */
761 if (NULL == var_size)
762 TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
763 * BITS_PER_UNIT, 0L);
764 else if (const_size == 0)
765 TYPE_SIZE (rec) = var_size;
766 else
767 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
768 round_up (bitsize_int (const_size, 0L),
769 BITS_PER_UNIT));
771 /* Determine the desired alignment. */
772 #ifdef ROUND_TYPE_ALIGN
773 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
774 #else
775 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
776 #endif
778 #ifdef ROUND_TYPE_SIZE
779 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
780 #else
781 /* Round the size up to be a multiple of the required alignment */
782 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
783 #endif
786 /* Calculate the mode, size, and alignment for TYPE.
787 For an array type, calculate the element separation as well.
788 Record TYPE on the chain of permanent or temporary types
789 so that dbxout will find out about it.
791 TYPE_SIZE of a type is nonzero if the type has been laid out already.
792 layout_type does nothing on such a type.
794 If the type is incomplete, its TYPE_SIZE remains zero. */
796 void
797 layout_type (type)
798 tree type;
800 int old;
801 tree pending_statics;
803 if (type == 0)
804 abort ();
806 /* Do nothing if type has been laid out before. */
807 if (TYPE_SIZE (type))
808 return;
810 /* Make sure all nodes we allocate are not momentary;
811 they must last past the current statement. */
812 old = suspend_momentary ();
814 /* Put all our nodes into the same obstack as the type. Also,
815 make expressions saveable (this is a no-op for permanent types). */
817 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
818 saveable_allocation ();
820 switch (TREE_CODE (type))
822 case LANG_TYPE:
823 /* This kind of type is the responsibility
824 of the language-specific code. */
825 abort ();
827 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
828 if (TYPE_PRECISION (type) == 0)
829 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
831 /* ... fall through ... */
833 case INTEGER_TYPE:
834 case ENUMERAL_TYPE:
835 case CHAR_TYPE:
836 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
837 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
838 TREE_UNSIGNED (type) = 1;
840 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
841 MODE_INT);
842 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
843 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
844 break;
846 case REAL_TYPE:
847 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
848 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
849 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
850 break;
852 case COMPLEX_TYPE:
853 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
854 TYPE_MODE (type)
855 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
856 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
857 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
859 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
860 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
861 break;
863 case VOID_TYPE:
864 TYPE_SIZE (type) = size_zero_node;
865 TYPE_SIZE_UNIT (type) = size_zero_node;
866 TYPE_ALIGN (type) = 1;
867 TYPE_MODE (type) = VOIDmode;
868 break;
870 case OFFSET_TYPE:
871 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
872 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
873 TYPE_MODE (type) = ptr_mode;
874 break;
876 case FUNCTION_TYPE:
877 case METHOD_TYPE:
878 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
879 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0);
880 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
881 break;
883 case POINTER_TYPE:
884 case REFERENCE_TYPE:
885 TYPE_MODE (type) = ptr_mode;
886 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
887 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
888 TREE_UNSIGNED (type) = 1;
889 TYPE_PRECISION (type) = POINTER_SIZE;
890 break;
892 case ARRAY_TYPE:
894 register tree index = TYPE_DOMAIN (type);
895 register tree element = TREE_TYPE (type);
897 build_pointer_type (element);
899 /* We need to know both bounds in order to compute the size. */
900 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
901 && TYPE_SIZE (element))
903 tree ub = TYPE_MAX_VALUE (index);
904 tree lb = TYPE_MIN_VALUE (index);
905 tree length;
906 tree element_size;
908 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
909 test for negative below covers it. */
910 if (TREE_CODE (ub) == MAX_EXPR
911 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
912 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
913 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
914 lb, 0))
915 ub = TREE_OPERAND (ub, 1);
916 else if (TREE_CODE (ub) == MAX_EXPR
917 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
918 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
919 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
921 lb, 0))
922 ub = TREE_OPERAND (ub, 0);
924 /* The initial subtraction should happen in the original type so
925 that (possible) negative values are handled appropriately. */
926 length = size_binop (PLUS_EXPR, size_one_node,
927 fold (build (MINUS_EXPR, TREE_TYPE (lb),
928 ub, lb)));
930 /* If neither bound is a constant and sizetype is signed, make
931 sure the size is never negative. We should really do this
932 if *either* bound is non-constant, but this is the best
933 compromise between C and Ada. */
934 if (! TREE_UNSIGNED (sizetype)
935 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
936 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
937 length = size_binop (MAX_EXPR, length, size_zero_node);
939 /* Special handling for arrays of bits (for Chill). */
940 element_size = TYPE_SIZE (element);
941 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
943 HOST_WIDE_INT maxvalue
944 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
945 HOST_WIDE_INT minvalue
946 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
948 if (maxvalue - minvalue == 1
949 && (maxvalue == 1 || maxvalue == 0))
950 element_size = integer_one_node;
953 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length);
955 /* If we know the size of the element, calculate the total
956 size directly, rather than do some division thing below.
957 This optimization helps Fortran assumed-size arrays
958 (where the size of the array is determined at runtime)
959 substantially.
960 Note that we can't do this in the case where the size of
961 the elements is one bit since TYPE_SIZE_UNIT cannot be
962 set correctly in that case. */
963 if (TYPE_SIZE_UNIT (element) != 0
964 && element_size != integer_one_node)
965 TYPE_SIZE_UNIT (type)
966 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
969 /* Now round the alignment and size,
970 using machine-dependent criteria if any. */
972 #ifdef ROUND_TYPE_ALIGN
973 TYPE_ALIGN (type)
974 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
975 #else
976 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
977 #endif
979 #ifdef ROUND_TYPE_SIZE
980 if (TYPE_SIZE (type) != 0)
982 tree tmp
983 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
985 /* If the rounding changed the size of the type, remove any
986 pre-calculated TYPE_SIZE_UNIT. */
987 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
988 TYPE_SIZE_UNIT (type) = NULL;
990 TYPE_SIZE (type) = tmp;
992 #endif
994 TYPE_MODE (type) = BLKmode;
995 if (TYPE_SIZE (type) != 0
996 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
997 /* BLKmode elements force BLKmode aggregate;
998 else extract/store fields may lose. */
999 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1000 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1002 TYPE_MODE (type)
1003 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1004 MODE_INT, 1);
1006 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1007 && ((int) TYPE_ALIGN (type)
1008 < TREE_INT_CST_LOW (TYPE_SIZE (type)))
1009 && TYPE_MODE (type) != BLKmode)
1011 TYPE_NO_FORCE_BLK (type) = 1;
1012 TYPE_MODE (type) = BLKmode;
1015 break;
1018 case RECORD_TYPE:
1019 pending_statics = layout_record (type);
1020 TYPE_MODE (type) = BLKmode;
1021 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
1023 tree field;
1024 enum machine_mode mode = VOIDmode;
1026 /* A record which has any BLKmode members must itself be BLKmode;
1027 it can't go in a register.
1028 Unless the member is BLKmode only because it isn't aligned. */
1029 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1031 int bitpos;
1033 if (TREE_CODE (field) != FIELD_DECL
1034 || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1035 continue;
1037 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1038 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1039 goto record_lose;
1041 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
1042 goto record_lose;
1044 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
1046 /* Must be BLKmode if any field crosses a word boundary,
1047 since extract_bit_field can't handle that in registers. */
1048 if (bitpos / BITS_PER_WORD
1049 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
1050 / BITS_PER_WORD)
1051 /* But there is no problem if the field is entire words. */
1052 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
1053 goto record_lose;
1055 /* If this field is the whole struct, remember its mode so
1056 that, say, we can put a double in a class into a DF
1057 register instead of forcing it to live in the stack. */
1058 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1059 mode = DECL_MODE (field);
1061 #ifdef STRUCT_FORCE_BLK
1062 /* With some targets, eg. c4x, it is sub-optimal
1063 to access an aligned BLKmode structure as a scalar. */
1064 if (mode == VOIDmode && STRUCT_FORCE_BLK (field))
1065 goto record_lose;
1066 #endif /* STRUCT_FORCE_BLK */
1069 if (mode != VOIDmode)
1070 /* We only have one real field; use its mode. */
1071 TYPE_MODE (type) = mode;
1072 else
1073 TYPE_MODE (type)
1074 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1075 MODE_INT, 1);
1077 /* If structure's known alignment is less than
1078 what the scalar mode would need, and it matters,
1079 then stick with BLKmode. */
1080 if (STRICT_ALIGNMENT
1081 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1082 || ((int) TYPE_ALIGN (type)
1083 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
1085 if (TYPE_MODE (type) != BLKmode)
1086 /* If this is the only reason this type is BLKmode,
1087 then don't force containing types to be BLKmode. */
1088 TYPE_NO_FORCE_BLK (type) = 1;
1089 TYPE_MODE (type) = BLKmode;
1092 record_lose: ;
1095 /* Lay out any static members. This is done now
1096 because their type may use the record's type. */
1097 while (pending_statics)
1099 layout_decl (TREE_VALUE (pending_statics), 0);
1100 pending_statics = TREE_CHAIN (pending_statics);
1102 break;
1104 case UNION_TYPE:
1105 case QUAL_UNION_TYPE:
1106 layout_union (type);
1107 TYPE_MODE (type) = BLKmode;
1108 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1109 /* If structure's known alignment is less than
1110 what the scalar mode would need, and it matters,
1111 then stick with BLKmode. */
1112 && (! STRICT_ALIGNMENT
1113 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1114 || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1116 tree field;
1117 /* A union which has any BLKmode members must itself be BLKmode;
1118 it can't go in a register.
1119 Unless the member is BLKmode only because it isn't aligned. */
1120 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1122 if (TREE_CODE (field) != FIELD_DECL)
1123 continue;
1125 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1126 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1127 goto union_lose;
1130 TYPE_MODE (type)
1131 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1132 MODE_INT, 1);
1134 union_lose: ;
1136 break;
1138 case SET_TYPE: /* Used by Chill and Pascal. */
1139 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1140 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1141 abort();
1142 else
1144 #ifndef SET_WORD_SIZE
1145 #define SET_WORD_SIZE BITS_PER_WORD
1146 #endif
1147 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1148 int size_in_bits
1149 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1150 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1151 int rounded_size
1152 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1153 if (rounded_size > alignment)
1154 TYPE_MODE (type) = BLKmode;
1155 else
1156 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1157 TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1158 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1159 TYPE_ALIGN (type) = alignment;
1160 TYPE_PRECISION (type) = size_in_bits;
1162 break;
1164 case FILE_TYPE:
1165 /* The size may vary in different languages, so the language front end
1166 should fill in the size. */
1167 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1168 TYPE_MODE (type) = BLKmode;
1169 break;
1171 default:
1172 abort ();
1173 } /* end switch */
1175 /* Normally, use the alignment corresponding to the mode chosen.
1176 However, where strict alignment is not required, avoid
1177 over-aligning structures, since most compilers do not do this
1178 alignment. */
1180 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1181 && (STRICT_ALIGNMENT
1182 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1183 && TREE_CODE (type) != QUAL_UNION_TYPE
1184 && TREE_CODE (type) != ARRAY_TYPE)))
1185 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1187 /* Do machine-dependent extra alignment. */
1188 #ifdef ROUND_TYPE_ALIGN
1189 TYPE_ALIGN (type)
1190 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1191 #endif
1193 #ifdef ROUND_TYPE_SIZE
1194 if (TYPE_SIZE (type) != 0)
1195 TYPE_SIZE (type)
1196 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1197 #endif
1199 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1200 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1201 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1203 /* If we failed to find a simple way to calculate the unit size
1204 of the type above, find it by division. */
1205 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1206 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1207 result will fit in sizetype. We will get more efficient code using
1208 sizetype, so we force a conversion. */
1209 TYPE_SIZE_UNIT (type)
1210 = convert (sizetype,
1211 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1212 size_int (BITS_PER_UNIT)));
1214 /* Once again evaluate only once, either now or as soon as safe. */
1215 if (TYPE_SIZE_UNIT (type) != 0
1216 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1217 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1219 /* Also layout any other variants of the type. */
1220 if (TYPE_NEXT_VARIANT (type)
1221 || type != TYPE_MAIN_VARIANT (type))
1223 tree variant;
1224 /* Record layout info of this variant. */
1225 tree size = TYPE_SIZE (type);
1226 tree size_unit = TYPE_SIZE_UNIT (type);
1227 int align = TYPE_ALIGN (type);
1228 enum machine_mode mode = TYPE_MODE (type);
1230 /* Copy it into all variants. */
1231 for (variant = TYPE_MAIN_VARIANT (type);
1232 variant != 0;
1233 variant = TYPE_NEXT_VARIANT (variant))
1235 TYPE_SIZE (variant) = size;
1236 TYPE_SIZE_UNIT (variant) = size_unit;
1237 TYPE_ALIGN (variant) = align;
1238 TYPE_MODE (variant) = mode;
1242 pop_obstacks ();
1243 resume_momentary (old);
1246 /* Create and return a type for signed integers of PRECISION bits. */
1248 tree
1249 make_signed_type (precision)
1250 int precision;
1252 register tree type = make_node (INTEGER_TYPE);
1254 TYPE_PRECISION (type) = precision;
1256 /* Create the extreme values based on the number of bits. */
1258 TYPE_MIN_VALUE (type)
1259 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1260 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1261 (((HOST_WIDE_INT) (-1)
1262 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1263 ? precision - HOST_BITS_PER_WIDE_INT - 1
1264 : 0))));
1265 TYPE_MAX_VALUE (type)
1266 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1267 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1268 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1269 ? (((HOST_WIDE_INT) 1
1270 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1271 : 0));
1273 /* Give this type's extreme values this type as their type. */
1275 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1276 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1278 /* The first type made with this or `make_unsigned_type'
1279 is the type for size values. */
1280 if (sizetype == 0)
1281 set_sizetype (type);
1283 /* Lay out the type: set its alignment, size, etc. */
1285 layout_type (type);
1286 return type;
1289 /* Create and return a type for unsigned integers of PRECISION bits. */
1291 tree
1292 make_unsigned_type (precision)
1293 int precision;
1295 register tree type = make_node (INTEGER_TYPE);
1297 TYPE_PRECISION (type) = precision;
1299 /* The first type made with this or `make_signed_type'
1300 is the type for size values. */
1302 if (sizetype == 0)
1304 TREE_UNSIGNED (type) = 1;
1305 set_sizetype (type);
1308 fixup_unsigned_type (type);
1309 return type;
1312 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1313 Also update the type of any standard type's sizes made so far. */
1315 void
1316 set_sizetype (type)
1317 tree type;
1319 int oprecision = TYPE_PRECISION (type);
1320 /* The *bitsizetype types use a precision that avoids overflows when
1321 calculating signed sizes / offsets in bits. However, when
1322 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1323 precision. */
1324 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1325 2 * HOST_BITS_PER_WIDE_INT);
1327 sizetype = type;
1328 bitsizetype = make_node (INTEGER_TYPE);
1329 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1330 TYPE_PRECISION (bitsizetype) = precision;
1332 if (TREE_UNSIGNED (type))
1333 fixup_unsigned_type (bitsizetype);
1334 else
1335 fixup_signed_type (bitsizetype);
1337 layout_type (bitsizetype);
1339 if (TREE_UNSIGNED (type))
1341 usizetype = sizetype;
1342 ubitsizetype = bitsizetype;
1343 ssizetype = make_signed_type (oprecision);
1344 sbitsizetype = make_signed_type (precision);
1346 else
1348 ssizetype = sizetype;
1349 sbitsizetype = bitsizetype;
1350 usizetype = make_unsigned_type (oprecision);
1351 ubitsizetype = make_unsigned_type (precision);
1353 TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1355 ggc_add_tree_root ((tree *) &sizetype_tab,
1356 sizeof sizetype_tab / sizeof (tree));
1359 /* Set the extreme values of TYPE based on its precision in bits,
1360 then lay it out. Used when make_signed_type won't do
1361 because the tree code is not INTEGER_TYPE.
1362 E.g. for Pascal, when the -fsigned-char option is given. */
1364 void
1365 fixup_signed_type (type)
1366 tree type;
1368 register int precision = TYPE_PRECISION (type);
1370 TYPE_MIN_VALUE (type)
1371 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1372 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1373 (((HOST_WIDE_INT) (-1)
1374 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1375 ? precision - HOST_BITS_PER_WIDE_INT - 1
1376 : 0))));
1377 TYPE_MAX_VALUE (type)
1378 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1379 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1380 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1381 ? (((HOST_WIDE_INT) 1
1382 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1383 : 0));
1385 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1386 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1388 /* Lay out the type: set its alignment, size, etc. */
1390 layout_type (type);
1393 /* Set the extreme values of TYPE based on its precision in bits,
1394 then lay it out. This is used both in `make_unsigned_type'
1395 and for enumeral types. */
1397 void
1398 fixup_unsigned_type (type)
1399 tree type;
1401 register int precision = TYPE_PRECISION (type);
1403 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1404 TYPE_MAX_VALUE (type)
1405 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1406 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1407 precision - HOST_BITS_PER_WIDE_INT > 0
1408 ? ((unsigned HOST_WIDE_INT) ~0
1409 >> (HOST_BITS_PER_WIDE_INT
1410 - (precision - HOST_BITS_PER_WIDE_INT)))
1411 : 0);
1412 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1413 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1415 /* Lay out the type: set its alignment, size, etc. */
1417 layout_type (type);
1420 /* Find the best machine mode to use when referencing a bit field of length
1421 BITSIZE bits starting at BITPOS.
1423 The underlying object is known to be aligned to a boundary of ALIGN bits.
1424 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1425 larger than LARGEST_MODE (usually SImode).
1427 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1428 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1429 mode meeting these conditions.
1431 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1432 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1433 all the conditions. */
1435 enum machine_mode
1436 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1437 int bitsize, bitpos;
1438 int align;
1439 enum machine_mode largest_mode;
1440 int volatilep;
1442 enum machine_mode mode;
1443 int unit = 0;
1445 /* Find the narrowest integer mode that contains the bit field. */
1446 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1447 mode = GET_MODE_WIDER_MODE (mode))
1449 unit = GET_MODE_BITSIZE (mode);
1450 if ((bitpos % unit) + bitsize <= unit)
1451 break;
1454 if (mode == VOIDmode
1455 /* It is tempting to omit the following line
1456 if STRICT_ALIGNMENT is true.
1457 But that is incorrect, since if the bitfield uses part of 3 bytes
1458 and we use a 4-byte mode, we could get a spurious segv
1459 if the extra 4th byte is past the end of memory.
1460 (Though at least one Unix compiler ignores this problem:
1461 that on the Sequent 386 machine. */
1462 || MIN (unit, BIGGEST_ALIGNMENT) > align
1463 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1464 return VOIDmode;
1466 if (SLOW_BYTE_ACCESS && ! volatilep)
1468 enum machine_mode wide_mode = VOIDmode, tmode;
1470 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1471 tmode = GET_MODE_WIDER_MODE (tmode))
1473 unit = GET_MODE_BITSIZE (tmode);
1474 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1475 && unit <= BITS_PER_WORD
1476 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1477 && (largest_mode == VOIDmode
1478 || unit <= GET_MODE_BITSIZE (largest_mode)))
1479 wide_mode = tmode;
1482 if (wide_mode != VOIDmode)
1483 return wide_mode;
1486 return mode;
1489 /* This function is run once to initialize stor-layout.c. */
1491 void
1492 init_stor_layout_once ()
1494 ggc_add_tree_root (&pending_sizes, 1);