* cp-tree.h (struct language_function): Remove static_labelno.
[official-gcc.git] / gcc / stor-layout.c
blob1986074c7548f5da22177020ae777fd6330bc369
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"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "flags.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "toplev.h"
32 #include "ggc.h"
34 #define CEIL(x,y) (((x) + (y) - 1) / (y))
36 /* Data type for the expressions representing sizes of data types.
37 It is the first integer type laid out. */
39 struct sizetype_tab sizetype_tab;
41 /* If nonzero, this is an upper limit on alignment of structure fields.
42 The value is measured in bits. */
43 int maximum_field_alignment;
45 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
46 May be overridden by front-ends. */
47 int set_alignment = 0;
49 static tree layout_record PROTO((tree));
50 static void layout_union PROTO((tree));
52 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
54 static tree pending_sizes;
56 /* Nonzero means cannot safely call expand_expr now,
57 so put variable sizes onto `pending_sizes' instead. */
59 int immediate_size_expand;
61 tree
62 get_pending_sizes ()
64 tree chain = pending_sizes;
65 tree t;
67 /* Put each SAVE_EXPR into the current function. */
68 for (t = chain; t; t = TREE_CHAIN (t))
69 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
70 pending_sizes = 0;
71 return chain;
74 void
75 put_pending_sizes (chain)
76 tree chain;
78 if (pending_sizes)
79 abort ();
81 pending_sizes = chain;
84 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
85 to serve as the actual size-expression for a type or decl. */
87 tree
88 variable_size (size)
89 tree size;
91 /* If the language-processor is to take responsibility for variable-sized
92 items (e.g., languages which have elaboration procedures like Ada),
93 just return SIZE unchanged. Likewise for self-referential sizes. */
94 if (TREE_CONSTANT (size)
95 || global_bindings_p () < 0 || contains_placeholder_p (size))
96 return size;
98 size = save_expr (size);
100 if (global_bindings_p ())
102 if (TREE_CONSTANT (size))
103 error ("type size can't be explicitly evaluated");
104 else
105 error ("variable-size type declared outside of any function");
107 return size_int (1);
110 if (immediate_size_expand)
111 /* NULL_RTX is not defined; neither is the rtx type.
112 Also, we would like to pass const0_rtx here, but don't have it. */
113 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
114 VOIDmode, 0);
115 else if (current_function && current_function->x_dont_save_pending_sizes_p)
116 /* The front-end doesn't want us to keep a list of the expressions
117 that determine sizes for variable size objects. */
119 else
120 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
122 return size;
125 #ifndef MAX_FIXED_MODE_SIZE
126 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
127 #endif
129 /* Return the machine mode to use for a nonscalar of SIZE bits.
130 The mode must be in class CLASS, and have exactly that many bits.
131 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
132 be used. */
134 enum machine_mode
135 mode_for_size (size, class, limit)
136 unsigned int size;
137 enum mode_class class;
138 int limit;
140 register enum machine_mode mode;
142 if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
143 return BLKmode;
145 /* Get the first mode which has this size, in the specified class. */
146 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
147 mode = GET_MODE_WIDER_MODE (mode))
148 if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
149 return mode;
151 return BLKmode;
154 /* Similar, but never return BLKmode; return the narrowest mode that
155 contains at least the requested number of bits. */
157 enum machine_mode
158 smallest_mode_for_size (size, class)
159 unsigned int size;
160 enum mode_class class;
162 register enum machine_mode mode;
164 /* Get the first mode which has at least this size, in the
165 specified class. */
166 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
167 mode = GET_MODE_WIDER_MODE (mode))
168 if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
169 return mode;
171 abort ();
174 /* Find an integer mode of the exact same size, or BLKmode on failure. */
176 enum machine_mode
177 int_mode_for_mode (mode)
178 enum machine_mode mode;
180 switch (GET_MODE_CLASS (mode))
182 case MODE_INT:
183 case MODE_PARTIAL_INT:
184 break;
186 case MODE_COMPLEX_INT:
187 case MODE_COMPLEX_FLOAT:
188 case MODE_FLOAT:
189 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
190 break;
192 case MODE_RANDOM:
193 if (mode == BLKmode)
194 break;
195 /* FALLTHRU */
197 case MODE_CC:
198 default:
199 abort();
202 return mode;
205 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
207 tree
208 round_up (value, divisor)
209 tree value;
210 int divisor;
212 return size_binop (MULT_EXPR,
213 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
214 size_int (divisor));
217 /* Set the size, mode and alignment of a ..._DECL node.
218 TYPE_DECL does need this for C++.
219 Note that LABEL_DECL and CONST_DECL nodes do not need this,
220 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
221 Don't call layout_decl for them.
223 KNOWN_ALIGN is the amount of alignment we can assume this
224 decl has with no special effort. It is relevant only for FIELD_DECLs
225 and depends on the previous fields.
226 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
227 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
228 the record will be aligned to suit. */
230 void
231 layout_decl (decl, known_align)
232 tree decl;
233 unsigned known_align;
235 register tree type = TREE_TYPE (decl);
236 register enum tree_code code = TREE_CODE (decl);
237 int spec_size = DECL_FIELD_SIZE (decl);
239 if (code == CONST_DECL)
240 return;
242 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
243 && code != FIELD_DECL && code != TYPE_DECL)
244 abort ();
246 if (type == error_mark_node)
248 type = void_type_node;
249 spec_size = 0;
252 /* Usually the size and mode come from the data type without change. */
254 DECL_MODE (decl) = TYPE_MODE (type);
255 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
256 if (DECL_SIZE (decl) == 0)
257 DECL_SIZE (decl) = TYPE_SIZE (type);
259 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
261 if (spec_size == 0 && DECL_NAME (decl) != 0)
262 abort ();
264 /* Size is specified number of bits. */
265 DECL_SIZE (decl) = size_int (spec_size);
267 /* Force alignment required for the data type.
268 But if the decl itself wants greater alignment, don't override that.
269 Likewise, if the decl is packed, don't override it. */
270 else if (DECL_ALIGN (decl) == 0
271 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
272 DECL_ALIGN (decl) = TYPE_ALIGN (type);
274 /* See if we can use an ordinary integer mode for a bit-field. */
275 /* Conditions are: a fixed size that is correct for another mode
276 and occupying a complete byte or bytes on proper boundary. */
277 if (code == FIELD_DECL)
279 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
280 if (maximum_field_alignment != 0)
281 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl),
282 (unsigned)maximum_field_alignment);
283 else if (DECL_PACKED (decl))
284 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
287 if (DECL_BIT_FIELD (decl)
288 && TYPE_SIZE (type) != 0
289 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
290 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
292 register enum machine_mode xmode
293 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
295 if (xmode != BLKmode
296 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
298 DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode),
299 DECL_ALIGN (decl));
300 DECL_MODE (decl) = xmode;
301 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
302 /* This no longer needs to be accessed as a bit field. */
303 DECL_BIT_FIELD (decl) = 0;
307 /* Turn off DECL_BIT_FIELD if we won't need it set. */
308 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
309 && known_align % TYPE_ALIGN (type) == 0
310 && DECL_SIZE (decl) != 0
311 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
312 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
313 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
314 DECL_BIT_FIELD (decl) = 0;
316 /* Evaluate nonconstant size only once, either now or as soon as safe. */
317 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
318 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
321 /* Lay out a RECORD_TYPE type (a C struct).
322 This means laying out the fields, determining their positions,
323 and computing the overall size and required alignment of the record.
324 Note that if you set the TYPE_ALIGN before calling this
325 then the struct is aligned to at least that boundary.
327 If the type has basetypes, you must call layout_basetypes
328 before calling this function.
330 The return value is a list of static members of the record.
331 They still need to be laid out. */
333 static tree
334 layout_record (rec)
335 tree rec;
337 register tree field;
338 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
339 /* These must be laid out *after* the record is. */
340 tree pending_statics = NULL_TREE;
341 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
342 where CONST_SIZE is an integer
343 and VAR_SIZE is a tree expression.
344 If VAR_SIZE is null, the size is just CONST_SIZE.
345 Naturally we try to avoid using VAR_SIZE. */
346 register HOST_WIDE_INT const_size = 0;
347 register tree var_size = 0;
348 /* Once we start using VAR_SIZE, this is the maximum alignment
349 that we know VAR_SIZE has. */
350 register int var_align = BITS_PER_UNIT;
352 #ifdef STRUCTURE_SIZE_BOUNDARY
353 /* Packed structures don't need to have minimum size. */
354 if (! TYPE_PACKED (rec))
355 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
356 #endif
358 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
360 register int known_align = var_size ? var_align : const_size;
361 register int desired_align = 0;
363 /* If FIELD is static, then treat it like a separate variable,
364 not really like a structure field.
365 If it is a FUNCTION_DECL, it's a method.
366 In both cases, all we do is lay out the decl,
367 and we do it *after* the record is laid out. */
369 if (TREE_CODE (field) == VAR_DECL)
371 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
372 continue;
374 /* Enumerators and enum types which are local to this class need not
375 be laid out. Likewise for initialized constant fields. */
376 if (TREE_CODE (field) != FIELD_DECL)
377 continue;
379 /* Lay out the field so we know what alignment it needs.
380 For a packed field, use the alignment as specified,
381 disregarding what the type would want. */
382 if (DECL_PACKED (field))
383 desired_align = DECL_ALIGN (field);
384 layout_decl (field, known_align);
385 if (! DECL_PACKED (field))
386 desired_align = DECL_ALIGN (field);
387 /* Some targets (i.e. VMS) limit struct field alignment
388 to a lower boundary than alignment of variables. */
389 #ifdef BIGGEST_FIELD_ALIGNMENT
390 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
391 #endif
392 #ifdef ADJUST_FIELD_ALIGN
393 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
394 #endif
396 /* Record must have at least as much alignment as any field.
397 Otherwise, the alignment of the field within the record
398 is meaningless. */
400 #ifndef PCC_BITFIELD_TYPE_MATTERS
401 record_align = MAX (record_align, desired_align);
402 #else
403 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
404 && DECL_BIT_FIELD_TYPE (field)
405 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
407 /* For these machines, a zero-length field does not
408 affect the alignment of the structure as a whole.
409 It does, however, affect the alignment of the next field
410 within the structure. */
411 if (! integer_zerop (DECL_SIZE (field)))
412 record_align = MAX ((int)record_align, desired_align);
413 else if (! DECL_PACKED (field))
414 desired_align = TYPE_ALIGN (TREE_TYPE (field));
415 /* A named bit field of declared type `int'
416 forces the entire structure to have `int' alignment. */
417 if (DECL_NAME (field) != 0)
419 int type_align = TYPE_ALIGN (TREE_TYPE (field));
420 if (maximum_field_alignment != 0)
421 type_align = MIN (type_align, maximum_field_alignment);
422 else if (DECL_PACKED (field))
423 type_align = MIN (type_align, BITS_PER_UNIT);
425 record_align = MAX ((int)record_align, type_align);
428 else
429 record_align = MAX ((int)record_align, desired_align);
430 #endif
432 /* Does this field automatically have alignment it needs
433 by virtue of the fields that precede it and the record's
434 own alignment? */
436 if (const_size % desired_align != 0
437 || (var_align % desired_align != 0
438 && var_size != 0))
440 /* No, we need to skip space before this field.
441 Bump the cumulative size to multiple of field alignment. */
443 if (var_size == 0
444 || var_align % desired_align == 0)
445 const_size
446 = CEIL (const_size, desired_align) * desired_align;
447 else
449 if (const_size > 0)
450 var_size = size_binop (PLUS_EXPR, var_size,
451 bitsize_int (const_size, 0L));
452 const_size = 0;
453 var_size = round_up (var_size, desired_align);
454 var_align = MIN (var_align, desired_align);
458 #ifdef PCC_BITFIELD_TYPE_MATTERS
459 if (PCC_BITFIELD_TYPE_MATTERS
460 && TREE_CODE (field) == FIELD_DECL
461 && TREE_TYPE (field) != error_mark_node
462 && DECL_BIT_FIELD_TYPE (field)
463 && !DECL_PACKED (field)
464 && maximum_field_alignment == 0
465 && !integer_zerop (DECL_SIZE (field)))
467 int type_align = TYPE_ALIGN (TREE_TYPE (field));
468 register tree dsize = DECL_SIZE (field);
469 int field_size = TREE_INT_CST_LOW (dsize);
471 /* A bit field may not span more units of alignment of its type
472 than its type itself. Advance to next boundary if necessary. */
473 if (((const_size + field_size + type_align - 1) / type_align
474 - const_size / type_align)
475 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
476 const_size = CEIL (const_size, type_align) * type_align;
478 #endif
480 /* No existing machine description uses this parameter.
481 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
482 #ifdef BITFIELD_NBYTES_LIMITED
483 if (BITFIELD_NBYTES_LIMITED
484 && TREE_CODE (field) == FIELD_DECL
485 && TREE_TYPE (field) != error_mark_node
486 && DECL_BIT_FIELD_TYPE (field)
487 && !DECL_PACKED (field)
488 && !integer_zerop (DECL_SIZE (field)))
490 int type_align = TYPE_ALIGN (TREE_TYPE (field));
491 register tree dsize = DECL_SIZE (field);
492 int field_size = TREE_INT_CST_LOW (dsize);
494 if (maximum_field_alignment != 0)
495 type_align = MIN (type_align, maximum_field_alignment);
496 /* ??? This test is opposite the test in the containing if
497 statement, so this code is unreachable currently. */
498 else if (DECL_PACKED (field))
499 type_align = MIN (type_align, BITS_PER_UNIT);
501 /* A bit field may not span the unit of alignment of its type.
502 Advance to next boundary if necessary. */
503 /* ??? This code should match the code above for the
504 PCC_BITFIELD_TYPE_MATTERS case. */
505 if (const_size / type_align
506 != (const_size + field_size - 1) / type_align)
507 const_size = CEIL (const_size, type_align) * type_align;
509 #endif
511 /* Size so far becomes the position of this field. */
513 if (var_size && const_size)
514 DECL_FIELD_BITPOS (field)
515 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
516 else if (var_size)
517 DECL_FIELD_BITPOS (field) = var_size;
518 else
520 DECL_FIELD_BITPOS (field) = size_int (const_size);
522 /* If this field ended up more aligned than we thought it
523 would be (we approximate this by seeing if its position
524 changed), lay out the field again; perhaps we can use an
525 integral mode for it now. */
526 if (known_align != const_size)
527 layout_decl (field, const_size);
530 /* Now add size of this field to the size of the record. */
533 register tree dsize = DECL_SIZE (field);
535 /* This can happen when we have an invalid nested struct definition,
536 such as struct j { struct j { int i; } }. The error message is
537 printed in finish_struct. */
538 if (dsize == 0)
539 /* Do nothing. */;
540 else if (TREE_CODE (dsize) == INTEGER_CST
541 && ! TREE_CONSTANT_OVERFLOW (dsize)
542 && TREE_INT_CST_HIGH (dsize) == 0
543 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
544 /* Use const_size if there's no overflow. */
545 const_size += TREE_INT_CST_LOW (dsize);
546 else
548 if (var_size == 0)
549 var_size = dsize;
550 else
551 var_size = size_binop (PLUS_EXPR, var_size, dsize);
556 /* Work out the total size and alignment of the record
557 as one expression and store in the record type.
558 Round it up to a multiple of the record's alignment. */
560 if (var_size == 0)
562 TYPE_SIZE (rec) = size_int (const_size);
564 else
566 if (const_size)
567 var_size
568 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
569 TYPE_SIZE (rec) = var_size;
572 /* Determine the desired alignment. */
573 #ifdef ROUND_TYPE_ALIGN
574 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
575 #else
576 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
577 #endif
579 /* Record the un-rounded size in the binfo node. But first we check
580 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
581 if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
582 TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
584 #ifdef ROUND_TYPE_SIZE
585 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
586 #else
587 /* Round the size up to be a multiple of the required alignment */
588 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
589 #endif
591 return pending_statics;
594 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
595 Lay out all the fields, set their positions to zero,
596 and compute the size and alignment of the union (maximum of any field).
597 Note that if you set the TYPE_ALIGN before calling this
598 then the union align is aligned to at least that boundary. */
600 static void
601 layout_union (rec)
602 tree rec;
604 register tree field;
605 unsigned union_align = BITS_PER_UNIT;
607 /* The size of the union, based on the fields scanned so far,
608 is max (CONST_SIZE, VAR_SIZE).
609 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
610 register int const_size = 0;
611 register tree var_size = 0;
613 #ifdef STRUCTURE_SIZE_BOUNDARY
614 /* Packed structures don't need to have minimum size. */
615 if (! TYPE_PACKED (rec))
616 union_align = STRUCTURE_SIZE_BOUNDARY;
617 #endif
619 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
620 the reverse order in building the COND_EXPR that denotes its
621 size. We reverse them again later. */
622 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
623 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
625 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
627 /* Enums which are local to this class need not be laid out. */
628 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
629 continue;
631 layout_decl (field, 0);
632 DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
634 /* Union must be at least as aligned as any field requires. */
636 union_align = MAX (union_align, DECL_ALIGN (field));
638 #ifdef PCC_BITFIELD_TYPE_MATTERS
639 /* On the m88000, a bit field of declare type `int'
640 forces the entire union to have `int' alignment. */
641 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
642 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
643 #endif
645 if (TREE_CODE (rec) == UNION_TYPE)
647 /* Set union_size to max (decl_size, union_size).
648 There are more and less general ways to do this.
649 Use only CONST_SIZE unless forced to use VAR_SIZE. */
651 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
652 const_size
653 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
654 else if (var_size == 0)
655 var_size = DECL_SIZE (field);
656 else
657 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
659 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
660 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
661 DECL_SIZE (field),
662 var_size ? var_size : bitsize_int (0L, 0L)));
665 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
666 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
668 /* Determine the ultimate size of the union (in bytes). */
669 if (NULL == var_size)
670 TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
671 * BITS_PER_UNIT, 0L);
672 else if (const_size == 0)
673 TYPE_SIZE (rec) = var_size;
674 else
675 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
676 round_up (bitsize_int (const_size, 0L),
677 BITS_PER_UNIT));
679 /* Determine the desired alignment. */
680 #ifdef ROUND_TYPE_ALIGN
681 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
682 #else
683 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
684 #endif
686 #ifdef ROUND_TYPE_SIZE
687 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
688 #else
689 /* Round the size up to be a multiple of the required alignment */
690 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
691 #endif
694 /* Calculate the mode, size, and alignment for TYPE.
695 For an array type, calculate the element separation as well.
696 Record TYPE on the chain of permanent or temporary types
697 so that dbxout will find out about it.
699 TYPE_SIZE of a type is nonzero if the type has been laid out already.
700 layout_type does nothing on such a type.
702 If the type is incomplete, its TYPE_SIZE remains zero. */
704 void
705 layout_type (type)
706 tree type;
708 int old;
709 tree pending_statics;
711 if (type == 0)
712 abort ();
714 /* Do nothing if type has been laid out before. */
715 if (TYPE_SIZE (type))
716 return;
718 /* Make sure all nodes we allocate are not momentary;
719 they must last past the current statement. */
720 old = suspend_momentary ();
722 /* Put all our nodes into the same obstack as the type. Also,
723 make expressions saveable (this is a no-op for permanent types). */
725 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
726 saveable_allocation ();
728 switch (TREE_CODE (type))
730 case LANG_TYPE:
731 /* This kind of type is the responsibility
732 of the language-specific code. */
733 abort ();
735 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
736 if (TYPE_PRECISION (type) == 0)
737 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
738 /* ... fall through ... */
740 case INTEGER_TYPE:
741 case ENUMERAL_TYPE:
742 case CHAR_TYPE:
743 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
744 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
745 TREE_UNSIGNED (type) = 1;
747 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
748 MODE_INT);
749 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
750 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
751 break;
753 case REAL_TYPE:
754 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
755 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
756 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
757 break;
759 case COMPLEX_TYPE:
760 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
761 TYPE_MODE (type)
762 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
763 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
764 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
766 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
767 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
768 break;
770 case VOID_TYPE:
771 TYPE_SIZE (type) = size_zero_node;
772 TYPE_SIZE_UNIT (type) = size_zero_node;
773 TYPE_ALIGN (type) = 1;
774 TYPE_MODE (type) = VOIDmode;
775 break;
777 case OFFSET_TYPE:
778 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
779 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
780 TYPE_MODE (type) = ptr_mode;
781 break;
783 case FUNCTION_TYPE:
784 case METHOD_TYPE:
785 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
786 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0);
787 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
788 break;
790 case POINTER_TYPE:
791 case REFERENCE_TYPE:
792 TYPE_MODE (type) = ptr_mode;
793 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
794 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
795 TREE_UNSIGNED (type) = 1;
796 TYPE_PRECISION (type) = POINTER_SIZE;
797 break;
799 case ARRAY_TYPE:
801 register tree index = TYPE_DOMAIN (type);
802 register tree element = TREE_TYPE (type);
804 build_pointer_type (element);
806 /* We need to know both bounds in order to compute the size. */
807 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
808 && TYPE_SIZE (element))
810 tree ub = TYPE_MAX_VALUE (index);
811 tree lb = TYPE_MIN_VALUE (index);
812 tree length;
813 tree element_size;
815 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
816 test for negative below covers it. */
817 if (TREE_CODE (ub) == MAX_EXPR
818 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
819 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
820 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
821 lb, 0))
822 ub = TREE_OPERAND (ub, 1);
823 else if (TREE_CODE (ub) == MAX_EXPR
824 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
825 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
826 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
828 lb, 0))
829 ub = TREE_OPERAND (ub, 0);
831 /* The initial subtraction should happen in the original type so
832 that (possible) negative values are handled appropriately. */
833 length = size_binop (PLUS_EXPR, size_one_node,
834 fold (build (MINUS_EXPR, TREE_TYPE (lb),
835 ub, lb)));
837 /* If neither bound is a constant and sizetype is signed, make
838 sure the size is never negative. We should really do this
839 if *either* bound is non-constant, but this is the best
840 compromise between C and Ada. */
841 if (! TREE_UNSIGNED (sizetype)
842 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
843 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
844 length = size_binop (MAX_EXPR, length, size_zero_node);
846 /* Special handling for arrays of bits (for Chill). */
847 element_size = TYPE_SIZE (element);
848 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
850 HOST_WIDE_INT maxvalue, minvalue;
851 maxvalue = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
852 minvalue = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
853 if (maxvalue - minvalue == 1
854 && (maxvalue == 1 || maxvalue == 0))
855 element_size = integer_one_node;
858 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length);
860 /* If we know the size of the element, calculate the total
861 size directly, rather than do some division thing below.
862 This optimization helps Fortran assumed-size arrays
863 (where the size of the array is determined at runtime)
864 substantially.
865 Note that we can't do this in the case where the size of
866 the elements is one bit since TYPE_SIZE_UNIT cannot be
867 set correctly in that case. */
868 if (TYPE_SIZE_UNIT (element) != 0
869 && element_size != integer_one_node)
871 TYPE_SIZE_UNIT (type)
872 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
876 /* Now round the alignment and size,
877 using machine-dependent criteria if any. */
879 #ifdef ROUND_TYPE_ALIGN
880 TYPE_ALIGN (type)
881 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
882 #else
883 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
884 #endif
886 #ifdef ROUND_TYPE_SIZE
887 if (TYPE_SIZE (type) != 0)
889 tree tmp;
890 tmp = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
891 /* If the rounding changed the size of the type, remove any
892 pre-calculated TYPE_SIZE_UNIT. */
893 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
894 TYPE_SIZE_UNIT (type) = NULL;
895 TYPE_SIZE (type) = tmp;
897 #endif
899 TYPE_MODE (type) = BLKmode;
900 if (TYPE_SIZE (type) != 0
901 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
902 /* BLKmode elements force BLKmode aggregate;
903 else extract/store fields may lose. */
904 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
905 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
907 TYPE_MODE (type)
908 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
909 MODE_INT, 1);
911 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
912 && (int)TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
913 && TYPE_MODE (type) != BLKmode)
915 TYPE_NO_FORCE_BLK (type) = 1;
916 TYPE_MODE (type) = BLKmode;
919 break;
922 case RECORD_TYPE:
923 pending_statics = layout_record (type);
924 TYPE_MODE (type) = BLKmode;
925 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
927 tree field;
928 enum machine_mode mode = VOIDmode;
930 /* A record which has any BLKmode members must itself be BLKmode;
931 it can't go in a register.
932 Unless the member is BLKmode only because it isn't aligned. */
933 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
935 int bitpos;
937 if (TREE_CODE (field) != FIELD_DECL
938 || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
939 continue;
941 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
942 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
943 goto record_lose;
945 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
946 goto record_lose;
948 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
950 /* Must be BLKmode if any field crosses a word boundary,
951 since extract_bit_field can't handle that in registers. */
952 if (bitpos / BITS_PER_WORD
953 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
954 / BITS_PER_WORD)
955 /* But there is no problem if the field is entire words. */
956 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
957 goto record_lose;
959 /* If this field is the whole struct, remember its mode so
960 that, say, we can put a double in a class into a DF
961 register instead of forcing it to live in the stack. */
962 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
963 mode = DECL_MODE (field);
966 if (mode != VOIDmode)
967 /* We only have one real field; use its mode. */
968 TYPE_MODE (type) = mode;
969 else
970 TYPE_MODE (type)
971 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
972 MODE_INT, 1);
974 /* If structure's known alignment is less than
975 what the scalar mode would need, and it matters,
976 then stick with BLKmode. */
977 if (STRICT_ALIGNMENT
978 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
979 || ((int)TYPE_ALIGN (type)
980 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
982 if (TYPE_MODE (type) != BLKmode)
983 /* If this is the only reason this type is BLKmode,
984 then don't force containing types to be BLKmode. */
985 TYPE_NO_FORCE_BLK (type) = 1;
986 TYPE_MODE (type) = BLKmode;
989 record_lose: ;
992 /* Lay out any static members. This is done now
993 because their type may use the record's type. */
994 while (pending_statics)
996 layout_decl (TREE_VALUE (pending_statics), 0);
997 pending_statics = TREE_CHAIN (pending_statics);
999 break;
1001 case UNION_TYPE:
1002 case QUAL_UNION_TYPE:
1003 layout_union (type);
1004 TYPE_MODE (type) = BLKmode;
1005 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1006 /* If structure's known alignment is less than
1007 what the scalar mode would need, and it matters,
1008 then stick with BLKmode. */
1009 && (! STRICT_ALIGNMENT
1010 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1011 || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1013 tree field;
1014 /* A union which has any BLKmode members must itself be BLKmode;
1015 it can't go in a register.
1016 Unless the member is BLKmode only because it isn't aligned. */
1017 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1019 if (TREE_CODE (field) != FIELD_DECL)
1020 continue;
1022 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1023 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1024 goto union_lose;
1027 TYPE_MODE (type)
1028 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1029 MODE_INT, 1);
1031 union_lose: ;
1033 break;
1035 case SET_TYPE: /* Used by Chill and Pascal. */
1036 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1037 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1038 abort();
1039 else
1041 #ifndef SET_WORD_SIZE
1042 #define SET_WORD_SIZE BITS_PER_WORD
1043 #endif
1044 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1045 int size_in_bits
1046 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1047 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1048 int rounded_size
1049 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1050 if (rounded_size > alignment)
1051 TYPE_MODE (type) = BLKmode;
1052 else
1053 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1054 TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1055 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1056 TYPE_ALIGN (type) = alignment;
1057 TYPE_PRECISION (type) = size_in_bits;
1059 break;
1061 case FILE_TYPE:
1062 /* The size may vary in different languages, so the language front end
1063 should fill in the size. */
1064 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1065 TYPE_MODE (type) = BLKmode;
1066 break;
1068 default:
1069 abort ();
1070 } /* end switch */
1072 /* Normally, use the alignment corresponding to the mode chosen.
1073 However, where strict alignment is not required, avoid
1074 over-aligning structures, since most compilers do not do this
1075 alignment. */
1077 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1078 && (STRICT_ALIGNMENT
1079 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1080 && TREE_CODE (type) != QUAL_UNION_TYPE
1081 && TREE_CODE (type) != ARRAY_TYPE)))
1082 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1084 /* Do machine-dependent extra alignment. */
1085 #ifdef ROUND_TYPE_ALIGN
1086 TYPE_ALIGN (type)
1087 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1088 #endif
1090 #ifdef ROUND_TYPE_SIZE
1091 if (TYPE_SIZE (type) != 0)
1092 TYPE_SIZE (type)
1093 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1094 #endif
1096 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1097 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1098 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1100 /* If we failed to find a simple way to calculate the unit size
1101 of the type above, find it by division. */
1102 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1104 TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1105 size_int (BITS_PER_UNIT));
1108 /* Once again evaluate only once, either now or as soon as safe. */
1109 if (TYPE_SIZE_UNIT (type) != 0
1110 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1111 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1113 /* Also layout any other variants of the type. */
1114 if (TYPE_NEXT_VARIANT (type)
1115 || type != TYPE_MAIN_VARIANT (type))
1117 tree variant;
1118 /* Record layout info of this variant. */
1119 tree size = TYPE_SIZE (type);
1120 tree size_unit = TYPE_SIZE_UNIT (type);
1121 int align = TYPE_ALIGN (type);
1122 enum machine_mode mode = TYPE_MODE (type);
1124 /* Copy it into all variants. */
1125 for (variant = TYPE_MAIN_VARIANT (type);
1126 variant;
1127 variant = TYPE_NEXT_VARIANT (variant))
1129 TYPE_SIZE (variant) = size;
1130 TYPE_SIZE_UNIT (variant) = size_unit;
1131 TYPE_ALIGN (variant) = align;
1132 TYPE_MODE (variant) = mode;
1136 pop_obstacks ();
1137 resume_momentary (old);
1140 /* Create and return a type for signed integers of PRECISION bits. */
1142 tree
1143 make_signed_type (precision)
1144 int precision;
1146 register tree type = make_node (INTEGER_TYPE);
1148 TYPE_PRECISION (type) = precision;
1150 /* Create the extreme values based on the number of bits. */
1152 TYPE_MIN_VALUE (type)
1153 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1154 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1155 (((HOST_WIDE_INT) (-1)
1156 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1157 ? precision - HOST_BITS_PER_WIDE_INT - 1
1158 : 0))));
1159 TYPE_MAX_VALUE (type)
1160 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1161 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1162 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1163 ? (((HOST_WIDE_INT) 1
1164 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1165 : 0));
1167 /* Give this type's extreme values this type as their type. */
1169 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1170 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1172 /* The first type made with this or `make_unsigned_type'
1173 is the type for size values. */
1175 if (sizetype == 0)
1176 set_sizetype (type);
1178 /* Lay out the type: set its alignment, size, etc. */
1180 layout_type (type);
1182 return type;
1185 /* Create and return a type for unsigned integers of PRECISION bits. */
1187 tree
1188 make_unsigned_type (precision)
1189 int precision;
1191 register tree type = make_node (INTEGER_TYPE);
1193 TYPE_PRECISION (type) = precision;
1195 /* The first type made with this or `make_signed_type'
1196 is the type for size values. */
1198 if (sizetype == 0)
1200 TREE_UNSIGNED (type) = 1;
1201 set_sizetype (type);
1204 fixup_unsigned_type (type);
1205 return type;
1208 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1209 Also update the type of any standard type's sizes made so far. */
1211 void
1212 set_sizetype (type)
1213 tree type;
1215 int oprecision = TYPE_PRECISION (type), precision;
1217 sizetype = type;
1219 /* The *bitsizetype types use a precision that avoids overflows when
1220 calculating signed sizes / offsets in bits. */
1221 precision = oprecision + BITS_PER_UNIT_LOG + 1;
1222 /* However, when cross-compiling from a 32 bit to a 64 bit host,
1223 we are limited to 64 bit precision. */
1224 if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1225 precision = 2 * HOST_BITS_PER_WIDE_INT;
1227 bitsizetype = make_node (INTEGER_TYPE);
1228 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1229 TYPE_PRECISION (bitsizetype) = precision;
1230 if (TREE_UNSIGNED (type))
1231 fixup_unsigned_type (bitsizetype);
1232 else
1233 fixup_signed_type (bitsizetype);
1234 layout_type (bitsizetype);
1236 if (TREE_UNSIGNED (type))
1238 usizetype = sizetype;
1239 ubitsizetype = bitsizetype;
1240 ssizetype = make_signed_type (oprecision);
1241 sbitsizetype = make_signed_type (precision);
1243 else
1245 ssizetype = sizetype;
1246 sbitsizetype = bitsizetype;
1247 usizetype = make_unsigned_type (oprecision);
1248 ubitsizetype = make_unsigned_type (precision);
1250 TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1252 ggc_add_tree_root ((tree*) &sizetype_tab, sizeof(sizetype_tab)/sizeof(tree));
1255 /* Set the extreme values of TYPE based on its precision in bits,
1256 then lay it out. Used when make_signed_type won't do
1257 because the tree code is not INTEGER_TYPE.
1258 E.g. for Pascal, when the -fsigned-char option is given. */
1260 void
1261 fixup_signed_type (type)
1262 tree type;
1264 register int precision = TYPE_PRECISION (type);
1266 TYPE_MIN_VALUE (type)
1267 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1268 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1269 (((HOST_WIDE_INT) (-1)
1270 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1271 ? precision - HOST_BITS_PER_WIDE_INT - 1
1272 : 0))));
1273 TYPE_MAX_VALUE (type)
1274 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1275 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1276 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1277 ? (((HOST_WIDE_INT) 1
1278 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1279 : 0));
1281 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1282 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1284 /* Lay out the type: set its alignment, size, etc. */
1286 layout_type (type);
1289 /* Set the extreme values of TYPE based on its precision in bits,
1290 then lay it out. This is used both in `make_unsigned_type'
1291 and for enumeral types. */
1293 void
1294 fixup_unsigned_type (type)
1295 tree type;
1297 register int precision = TYPE_PRECISION (type);
1299 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1300 TYPE_MAX_VALUE (type)
1301 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1302 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1303 precision - HOST_BITS_PER_WIDE_INT > 0
1304 ? ((unsigned HOST_WIDE_INT) ~0
1305 >> (HOST_BITS_PER_WIDE_INT
1306 - (precision - HOST_BITS_PER_WIDE_INT)))
1307 : 0);
1308 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1309 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1311 /* Lay out the type: set its alignment, size, etc. */
1313 layout_type (type);
1316 /* Find the best machine mode to use when referencing a bit field of length
1317 BITSIZE bits starting at BITPOS.
1319 The underlying object is known to be aligned to a boundary of ALIGN bits.
1320 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1321 larger than LARGEST_MODE (usually SImode).
1323 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1324 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1325 mode meeting these conditions.
1327 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1328 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1329 all the conditions. */
1331 enum machine_mode
1332 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1333 int bitsize, bitpos;
1334 int align;
1335 enum machine_mode largest_mode;
1336 int volatilep;
1338 enum machine_mode mode;
1339 int unit = 0;
1341 /* Find the narrowest integer mode that contains the bit field. */
1342 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1343 mode = GET_MODE_WIDER_MODE (mode))
1345 unit = GET_MODE_BITSIZE (mode);
1346 if ((bitpos % unit) + bitsize <= unit)
1347 break;
1350 if (mode == VOIDmode
1351 /* It is tempting to omit the following line
1352 if STRICT_ALIGNMENT is true.
1353 But that is incorrect, since if the bitfield uses part of 3 bytes
1354 and we use a 4-byte mode, we could get a spurious segv
1355 if the extra 4th byte is past the end of memory.
1356 (Though at least one Unix compiler ignores this problem:
1357 that on the Sequent 386 machine. */
1358 || MIN (unit, BIGGEST_ALIGNMENT) > align
1359 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1360 return VOIDmode;
1362 if (SLOW_BYTE_ACCESS && ! volatilep)
1364 enum machine_mode wide_mode = VOIDmode, tmode;
1366 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1367 tmode = GET_MODE_WIDER_MODE (tmode))
1369 unit = GET_MODE_BITSIZE (tmode);
1370 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1371 && unit <= BITS_PER_WORD
1372 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1373 && (largest_mode == VOIDmode
1374 || unit <= GET_MODE_BITSIZE (largest_mode)))
1375 wide_mode = tmode;
1378 if (wide_mode != VOIDmode)
1379 return wide_mode;
1382 return mode;
1385 /* This function is run once to initialize stor-layout.c. */
1387 void
1388 init_stor_layout_once ()
1390 ggc_add_tree_root (&pending_sizes, 1);