--with-gnu-ld uses different x- fiile under aix 4.1
[official-gcc.git] / gcc / stor-layout.c
bloba712664843feda314ef5dae8ce1a1e14b7002edb
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 "flags.h"
28 #include "except.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "toplev.h"
33 #define CEIL(x,y) (((x) + (y) - 1) / (y))
35 /* Data type for the expressions representing sizes of data types.
36 It is the first integer type laid out. */
38 struct sizetype_tab sizetype_tab;
40 /* An integer constant with value 0 whose type is sizetype. */
42 tree size_zero_node;
44 /* An integer constant with value 1 whose type is sizetype. */
46 tree size_one_node;
48 /* If nonzero, this is an upper limit on alignment of structure fields.
49 The value is measured in bits. */
50 int maximum_field_alignment;
52 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
53 May be overridden by front-ends. */
54 int set_alignment = 0;
56 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
57 enum mode_class));
58 static tree layout_record PROTO((tree));
59 static void layout_union PROTO((tree));
61 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
63 static tree pending_sizes;
65 /* Nonzero means cannot safely call expand_expr now,
66 so put variable sizes onto `pending_sizes' instead. */
68 int immediate_size_expand;
70 tree
71 get_pending_sizes ()
73 tree chain = pending_sizes;
74 tree t;
76 /* Put each SAVE_EXPR into the current function. */
77 for (t = chain; t; t = TREE_CHAIN (t))
78 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
79 pending_sizes = 0;
80 return chain;
83 void
84 put_pending_sizes (chain)
85 tree chain;
87 if (pending_sizes)
88 abort ();
90 pending_sizes = chain;
93 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
94 to serve as the actual size-expression for a type or decl. */
96 tree
97 variable_size (size)
98 tree size;
100 /* If the language-processor is to take responsibility for variable-sized
101 items (e.g., languages which have elaboration procedures like Ada),
102 just return SIZE unchanged. Likewise for self-referential sizes. */
103 if (TREE_CONSTANT (size)
104 || global_bindings_p () < 0 || contains_placeholder_p (size))
105 return size;
107 size = save_expr (size);
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
125 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
127 return size;
130 #ifndef MAX_FIXED_MODE_SIZE
131 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
132 #endif
134 /* Return the machine mode to use for a nonscalar of SIZE bits.
135 The mode must be in class CLASS, and have exactly that many bits.
136 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
137 be used. */
139 enum machine_mode
140 mode_for_size (size, class, limit)
141 unsigned int size;
142 enum mode_class class;
143 int limit;
145 register enum machine_mode mode;
147 if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
148 return BLKmode;
150 /* Get the first mode which has this size, in the specified class. */
151 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
152 mode = GET_MODE_WIDER_MODE (mode))
153 if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
154 return mode;
156 return BLKmode;
159 /* Similar, but never return BLKmode; return the narrowest mode that
160 contains at least the requested number of bits. */
162 static enum machine_mode
163 smallest_mode_for_size (size, class)
164 unsigned int size;
165 enum mode_class class;
167 register enum machine_mode mode;
169 /* Get the first mode which has at least this size, in the
170 specified class. */
171 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
172 mode = GET_MODE_WIDER_MODE (mode))
173 if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
174 return mode;
176 abort ();
179 /* Find an integer mode of the exact same size, or BLKmode on failure. */
181 enum machine_mode
182 int_mode_for_mode (mode)
183 enum machine_mode mode;
185 switch (GET_MODE_CLASS (mode))
187 case MODE_INT:
188 case MODE_PARTIAL_INT:
189 break;
191 case MODE_COMPLEX_INT:
192 case MODE_COMPLEX_FLOAT:
193 case MODE_FLOAT:
194 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
195 break;
197 case MODE_RANDOM:
198 if (mode == BLKmode)
199 break;
200 /* FALLTHRU */
202 case MODE_CC:
203 default:
204 abort();
207 return mode;
210 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
212 tree
213 round_up (value, divisor)
214 tree value;
215 int divisor;
217 return size_binop (MULT_EXPR,
218 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
219 size_int (divisor));
222 /* Set the size, mode and alignment of a ..._DECL node.
223 TYPE_DECL does need this for C++.
224 Note that LABEL_DECL and CONST_DECL nodes do not need this,
225 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
226 Don't call layout_decl for them.
228 KNOWN_ALIGN is the amount of alignment we can assume this
229 decl has with no special effort. It is relevant only for FIELD_DECLs
230 and depends on the previous fields.
231 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
232 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
233 the record will be aligned to suit. */
235 void
236 layout_decl (decl, known_align)
237 tree decl;
238 unsigned known_align;
240 register tree type = TREE_TYPE (decl);
241 register enum tree_code code = TREE_CODE (decl);
242 int spec_size = DECL_FIELD_SIZE (decl);
244 if (code == CONST_DECL)
245 return;
247 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
248 && code != FIELD_DECL && code != TYPE_DECL)
249 abort ();
251 if (type == error_mark_node)
253 type = void_type_node;
254 spec_size = 0;
257 /* Usually the size and mode come from the data type without change. */
259 DECL_MODE (decl) = TYPE_MODE (type);
260 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
261 if (DECL_SIZE (decl) == 0)
262 DECL_SIZE (decl) = TYPE_SIZE (type);
264 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
266 if (spec_size == 0 && DECL_NAME (decl) != 0)
267 abort ();
269 /* Size is specified number of bits. */
270 DECL_SIZE (decl) = size_int (spec_size);
272 /* Force alignment required for the data type.
273 But if the decl itself wants greater alignment, don't override that.
274 Likewise, if the decl is packed, don't override it. */
275 else if (DECL_ALIGN (decl) == 0
276 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
277 DECL_ALIGN (decl) = TYPE_ALIGN (type);
279 /* See if we can use an ordinary integer mode for a bit-field. */
280 /* Conditions are: a fixed size that is correct for another mode
281 and occupying a complete byte or bytes on proper boundary. */
282 if (code == FIELD_DECL)
284 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
285 if (maximum_field_alignment != 0)
286 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl),
287 (unsigned)maximum_field_alignment);
288 else if (DECL_PACKED (decl))
289 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
292 if (DECL_BIT_FIELD (decl)
293 && TYPE_SIZE (type) != 0
294 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
295 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
297 register enum machine_mode xmode
298 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
300 if (xmode != BLKmode
301 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
303 DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode),
304 DECL_ALIGN (decl));
305 DECL_MODE (decl) = xmode;
306 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
307 /* This no longer needs to be accessed as a bit field. */
308 DECL_BIT_FIELD (decl) = 0;
312 /* Turn off DECL_BIT_FIELD if we won't need it set. */
313 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
314 && known_align % TYPE_ALIGN (type) == 0
315 && DECL_SIZE (decl) != 0
316 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
317 || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
318 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
319 DECL_BIT_FIELD (decl) = 0;
321 /* Evaluate nonconstant size only once, either now or as soon as safe. */
322 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
323 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
326 /* Lay out a RECORD_TYPE type (a C struct).
327 This means laying out the fields, determining their positions,
328 and computing the overall size and required alignment of the record.
329 Note that if you set the TYPE_ALIGN before calling this
330 then the struct is aligned to at least that boundary.
332 If the type has basetypes, you must call layout_basetypes
333 before calling this function.
335 The return value is a list of static members of the record.
336 They still need to be laid out. */
338 static tree
339 layout_record (rec)
340 tree rec;
342 register tree field;
343 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
344 /* These must be laid out *after* the record is. */
345 tree pending_statics = NULL_TREE;
346 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
347 where CONST_SIZE is an integer
348 and VAR_SIZE is a tree expression.
349 If VAR_SIZE is null, the size is just CONST_SIZE.
350 Naturally we try to avoid using VAR_SIZE. */
351 register HOST_WIDE_INT const_size = 0;
352 register tree var_size = 0;
353 /* Once we start using VAR_SIZE, this is the maximum alignment
354 that we know VAR_SIZE has. */
355 register int var_align = BITS_PER_UNIT;
357 #ifdef STRUCTURE_SIZE_BOUNDARY
358 /* Packed structures don't need to have minimum size. */
359 if (! TYPE_PACKED (rec))
360 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
361 #endif
363 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
365 register int known_align = var_size ? var_align : const_size;
366 register int desired_align = 0;
368 /* If FIELD is static, then treat it like a separate variable,
369 not really like a structure field.
370 If it is a FUNCTION_DECL, it's a method.
371 In both cases, all we do is lay out the decl,
372 and we do it *after* the record is laid out. */
374 if (TREE_CODE (field) == VAR_DECL)
376 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
377 continue;
379 /* Enumerators and enum types which are local to this class need not
380 be laid out. Likewise for initialized constant fields. */
381 if (TREE_CODE (field) != FIELD_DECL)
382 continue;
384 /* Lay out the field so we know what alignment it needs.
385 For a packed field, use the alignment as specified,
386 disregarding what the type would want. */
387 if (DECL_PACKED (field))
388 desired_align = DECL_ALIGN (field);
389 layout_decl (field, known_align);
390 if (! DECL_PACKED (field))
391 desired_align = DECL_ALIGN (field);
392 /* Some targets (i.e. VMS) limit struct field alignment
393 to a lower boundary than alignment of variables. */
394 #ifdef BIGGEST_FIELD_ALIGNMENT
395 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
396 #endif
397 #ifdef ADJUST_FIELD_ALIGN
398 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
399 #endif
401 /* Record must have at least as much alignment as any field.
402 Otherwise, the alignment of the field within the record
403 is meaningless. */
405 #ifndef PCC_BITFIELD_TYPE_MATTERS
406 record_align = MAX (record_align, desired_align);
407 #else
408 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
409 && DECL_BIT_FIELD_TYPE (field)
410 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
412 /* For these machines, a zero-length field does not
413 affect the alignment of the structure as a whole.
414 It does, however, affect the alignment of the next field
415 within the structure. */
416 if (! integer_zerop (DECL_SIZE (field)))
417 record_align = MAX ((int)record_align, desired_align);
418 else if (! DECL_PACKED (field))
419 desired_align = TYPE_ALIGN (TREE_TYPE (field));
420 /* A named bit field of declared type `int'
421 forces the entire structure to have `int' alignment. */
422 if (DECL_NAME (field) != 0)
424 int type_align = TYPE_ALIGN (TREE_TYPE (field));
425 if (maximum_field_alignment != 0)
426 type_align = MIN (type_align, maximum_field_alignment);
427 else if (DECL_PACKED (field))
428 type_align = MIN (type_align, BITS_PER_UNIT);
430 record_align = MAX ((int)record_align, type_align);
433 else
434 record_align = MAX ((int)record_align, desired_align);
435 #endif
437 /* Does this field automatically have alignment it needs
438 by virtue of the fields that precede it and the record's
439 own alignment? */
441 if (const_size % desired_align != 0
442 || (var_align % desired_align != 0
443 && var_size != 0))
445 /* No, we need to skip space before this field.
446 Bump the cumulative size to multiple of field alignment. */
448 if (var_size == 0
449 || var_align % desired_align == 0)
450 const_size
451 = CEIL (const_size, desired_align) * desired_align;
452 else
454 if (const_size > 0)
455 var_size = size_binop (PLUS_EXPR, var_size,
456 bitsize_int (const_size, 0L));
457 const_size = 0;
458 var_size = round_up (var_size, desired_align);
459 var_align = MIN (var_align, desired_align);
463 #ifdef PCC_BITFIELD_TYPE_MATTERS
464 if (PCC_BITFIELD_TYPE_MATTERS
465 && TREE_CODE (field) == FIELD_DECL
466 && TREE_TYPE (field) != error_mark_node
467 && DECL_BIT_FIELD_TYPE (field)
468 && !DECL_PACKED (field)
469 && maximum_field_alignment == 0
470 && !integer_zerop (DECL_SIZE (field)))
472 int type_align = TYPE_ALIGN (TREE_TYPE (field));
473 register tree dsize = DECL_SIZE (field);
474 int field_size = TREE_INT_CST_LOW (dsize);
476 /* A bit field may not span more units of alignment of its type
477 than its type itself. Advance to next boundary if necessary. */
478 if (((const_size + field_size + type_align - 1) / type_align
479 - const_size / type_align)
480 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
481 const_size = CEIL (const_size, type_align) * type_align;
483 #endif
485 /* No existing machine description uses this parameter.
486 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
487 #ifdef BITFIELD_NBYTES_LIMITED
488 if (BITFIELD_NBYTES_LIMITED
489 && TREE_CODE (field) == FIELD_DECL
490 && TREE_TYPE (field) != error_mark_node
491 && DECL_BIT_FIELD_TYPE (field)
492 && !DECL_PACKED (field)
493 && !integer_zerop (DECL_SIZE (field)))
495 int type_align = TYPE_ALIGN (TREE_TYPE (field));
496 register tree dsize = DECL_SIZE (field);
497 int field_size = TREE_INT_CST_LOW (dsize);
499 if (maximum_field_alignment != 0)
500 type_align = MIN (type_align, maximum_field_alignment);
501 /* ??? This test is opposite the test in the containing if
502 statement, so this code is unreachable currently. */
503 else if (DECL_PACKED (field))
504 type_align = MIN (type_align, BITS_PER_UNIT);
506 /* A bit field may not span the unit of alignment of its type.
507 Advance to next boundary if necessary. */
508 /* ??? This code should match the code above for the
509 PCC_BITFIELD_TYPE_MATTERS case. */
510 if (const_size / type_align
511 != (const_size + field_size - 1) / type_align)
512 const_size = CEIL (const_size, type_align) * type_align;
514 #endif
516 /* Size so far becomes the position of this field. */
518 if (var_size && const_size)
519 DECL_FIELD_BITPOS (field)
520 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
521 else if (var_size)
522 DECL_FIELD_BITPOS (field) = var_size;
523 else
525 DECL_FIELD_BITPOS (field) = size_int (const_size);
527 /* If this field ended up more aligned than we thought it
528 would be (we approximate this by seeing if its position
529 changed), lay out the field again; perhaps we can use an
530 integral mode for it now. */
531 if (known_align != const_size)
532 layout_decl (field, const_size);
535 /* Now add size of this field to the size of the record. */
538 register tree dsize = DECL_SIZE (field);
540 /* This can happen when we have an invalid nested struct definition,
541 such as struct j { struct j { int i; } }. The error message is
542 printed in finish_struct. */
543 if (dsize == 0)
544 /* Do nothing. */;
545 else if (TREE_CODE (dsize) == INTEGER_CST
546 && ! TREE_CONSTANT_OVERFLOW (dsize)
547 && TREE_INT_CST_HIGH (dsize) == 0
548 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
549 /* Use const_size if there's no overflow. */
550 const_size += TREE_INT_CST_LOW (dsize);
551 else
553 if (var_size == 0)
554 var_size = dsize;
555 else
556 var_size = size_binop (PLUS_EXPR, var_size, dsize);
561 /* Work out the total size and alignment of the record
562 as one expression and store in the record type.
563 Round it up to a multiple of the record's alignment. */
565 if (var_size == 0)
567 TYPE_SIZE (rec) = size_int (const_size);
569 else
571 if (const_size)
572 var_size
573 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
574 TYPE_SIZE (rec) = var_size;
577 /* Determine the desired alignment. */
578 #ifdef ROUND_TYPE_ALIGN
579 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
580 #else
581 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
582 #endif
584 /* Record the un-rounded size in the binfo node. But first we check
585 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
586 if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
587 TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
589 #ifdef ROUND_TYPE_SIZE
590 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
591 #else
592 /* Round the size up to be a multiple of the required alignment */
593 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
594 #endif
596 return pending_statics;
599 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
600 Lay out all the fields, set their positions to zero,
601 and compute the size and alignment of the union (maximum of any field).
602 Note that if you set the TYPE_ALIGN before calling this
603 then the union align is aligned to at least that boundary. */
605 static void
606 layout_union (rec)
607 tree rec;
609 register tree field;
610 unsigned union_align = BITS_PER_UNIT;
612 /* The size of the union, based on the fields scanned so far,
613 is max (CONST_SIZE, VAR_SIZE).
614 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
615 register int const_size = 0;
616 register tree var_size = 0;
618 #ifdef STRUCTURE_SIZE_BOUNDARY
619 /* Packed structures don't need to have minimum size. */
620 if (! TYPE_PACKED (rec))
621 union_align = STRUCTURE_SIZE_BOUNDARY;
622 #endif
624 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
625 the reverse order in building the COND_EXPR that denotes its
626 size. We reverse them again later. */
627 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
628 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
630 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
632 /* Enums which are local to this class need not be laid out. */
633 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
634 continue;
636 layout_decl (field, 0);
637 DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
639 /* Union must be at least as aligned as any field requires. */
641 union_align = MAX (union_align, DECL_ALIGN (field));
643 #ifdef PCC_BITFIELD_TYPE_MATTERS
644 /* On the m88000, a bit field of declare type `int'
645 forces the entire union to have `int' alignment. */
646 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
647 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
648 #endif
650 if (TREE_CODE (rec) == UNION_TYPE)
652 /* Set union_size to max (decl_size, union_size).
653 There are more and less general ways to do this.
654 Use only CONST_SIZE unless forced to use VAR_SIZE. */
656 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
657 const_size
658 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
659 else if (var_size == 0)
660 var_size = DECL_SIZE (field);
661 else
662 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
664 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
665 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
666 DECL_SIZE (field),
667 var_size ? var_size : bitsize_int (0L, 0L)));
670 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
671 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
673 /* Determine the ultimate size of the union (in bytes). */
674 if (NULL == var_size)
675 TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
676 * BITS_PER_UNIT, 0L);
677 else if (const_size == 0)
678 TYPE_SIZE (rec) = var_size;
679 else
680 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
681 round_up (bitsize_int (const_size, 0L),
682 BITS_PER_UNIT));
684 /* Determine the desired alignment. */
685 #ifdef ROUND_TYPE_ALIGN
686 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
687 #else
688 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
689 #endif
691 #ifdef ROUND_TYPE_SIZE
692 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
693 #else
694 /* Round the size up to be a multiple of the required alignment */
695 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
696 #endif
699 /* Calculate the mode, size, and alignment for TYPE.
700 For an array type, calculate the element separation as well.
701 Record TYPE on the chain of permanent or temporary types
702 so that dbxout will find out about it.
704 TYPE_SIZE of a type is nonzero if the type has been laid out already.
705 layout_type does nothing on such a type.
707 If the type is incomplete, its TYPE_SIZE remains zero. */
709 void
710 layout_type (type)
711 tree type;
713 int old;
714 tree pending_statics;
716 if (type == 0)
717 abort ();
719 /* Do nothing if type has been laid out before. */
720 if (TYPE_SIZE (type))
721 return;
723 /* Make sure all nodes we allocate are not momentary;
724 they must last past the current statement. */
725 old = suspend_momentary ();
727 /* Put all our nodes into the same obstack as the type. Also,
728 make expressions saveable (this is a no-op for permanent types). */
730 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
731 saveable_allocation ();
733 switch (TREE_CODE (type))
735 case LANG_TYPE:
736 /* This kind of type is the responsibility
737 of the language-specific code. */
738 abort ();
740 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
741 if (TYPE_PRECISION (type) == 0)
742 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
743 /* ... fall through ... */
745 case INTEGER_TYPE:
746 case ENUMERAL_TYPE:
747 case CHAR_TYPE:
748 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
749 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
750 TREE_UNSIGNED (type) = 1;
752 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
753 MODE_INT);
754 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
755 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
756 break;
758 case REAL_TYPE:
759 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
760 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
761 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
762 break;
764 case COMPLEX_TYPE:
765 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
766 TYPE_MODE (type)
767 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
768 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
769 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
771 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
772 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
773 break;
775 case VOID_TYPE:
776 TYPE_SIZE (type) = size_zero_node;
777 TYPE_SIZE_UNIT (type) = size_zero_node;
778 TYPE_ALIGN (type) = 1;
779 TYPE_MODE (type) = VOIDmode;
780 break;
782 case OFFSET_TYPE:
783 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
784 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
785 TYPE_MODE (type) = ptr_mode;
786 break;
788 case FUNCTION_TYPE:
789 case METHOD_TYPE:
790 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
791 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0);
792 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
793 break;
795 case POINTER_TYPE:
796 case REFERENCE_TYPE:
797 TYPE_MODE (type) = ptr_mode;
798 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
799 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
800 TREE_UNSIGNED (type) = 1;
801 TYPE_PRECISION (type) = POINTER_SIZE;
802 break;
804 case ARRAY_TYPE:
806 register tree index = TYPE_DOMAIN (type);
807 register tree element = TREE_TYPE (type);
809 build_pointer_type (element);
811 /* We need to know both bounds in order to compute the size. */
812 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
813 && TYPE_SIZE (element))
815 tree ub = TYPE_MAX_VALUE (index);
816 tree lb = TYPE_MIN_VALUE (index);
817 tree length;
818 tree element_size;
820 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
821 test for negative below covers it. */
822 if (TREE_CODE (ub) == MAX_EXPR
823 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
824 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
825 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
826 lb, 0))
827 ub = TREE_OPERAND (ub, 1);
828 else if (TREE_CODE (ub) == MAX_EXPR
829 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
830 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
831 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
833 lb, 0))
834 ub = TREE_OPERAND (ub, 0);
836 /* The initial subtraction should happen in the original type so
837 that (possible) negative values are handled appropriately. */
838 length = size_binop (PLUS_EXPR, size_one_node,
839 fold (build (MINUS_EXPR, TREE_TYPE (lb),
840 ub, lb)));
842 /* If neither bound is a constant and sizetype is signed, make
843 sure the size is never negative. We should really do this
844 if *either* bound is non-constant, but this is the best
845 compromise between C and Ada. */
846 if (! TREE_UNSIGNED (sizetype)
847 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
848 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
849 length = size_binop (MAX_EXPR, length, size_zero_node);
851 /* Special handling for arrays of bits (for Chill). */
852 element_size = TYPE_SIZE (element);
853 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
855 HOST_WIDE_INT maxvalue, minvalue;
856 maxvalue = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
857 minvalue = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
858 if (maxvalue - minvalue == 1
859 && (maxvalue == 1 || maxvalue == 0))
860 element_size = integer_one_node;
863 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length);
865 /* If we know the size of the element, calculate the total
866 size directly, rather than do some division thing below.
867 This optimization helps Fortran assumed-size arrays
868 (where the size of the array is determined at runtime)
869 substantially.
870 Note that we can't do this in the case where the size of
871 the elements is one bit since TYPE_SIZE_UNIT cannot be
872 set correctly in that case. */
873 if (TYPE_SIZE_UNIT (element) != 0
874 && element_size != integer_one_node)
876 TYPE_SIZE_UNIT (type)
877 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
881 /* Now round the alignment and size,
882 using machine-dependent criteria if any. */
884 #ifdef ROUND_TYPE_ALIGN
885 TYPE_ALIGN (type)
886 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
887 #else
888 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
889 #endif
891 #ifdef ROUND_TYPE_SIZE
892 if (TYPE_SIZE (type) != 0)
894 tree tmp;
895 tmp = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
896 /* If the rounding changed the size of the type, remove any
897 pre-calculated TYPE_SIZE_UNIT. */
898 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
899 TYPE_SIZE_UNIT (type) = NULL;
900 TYPE_SIZE (type) = tmp;
902 #endif
904 TYPE_MODE (type) = BLKmode;
905 if (TYPE_SIZE (type) != 0
906 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
907 /* BLKmode elements force BLKmode aggregate;
908 else extract/store fields may lose. */
909 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
910 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
912 TYPE_MODE (type)
913 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
914 MODE_INT, 1);
916 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
917 && (int)TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
918 && TYPE_MODE (type) != BLKmode)
920 TYPE_NO_FORCE_BLK (type) = 1;
921 TYPE_MODE (type) = BLKmode;
924 break;
927 case RECORD_TYPE:
928 pending_statics = layout_record (type);
929 TYPE_MODE (type) = BLKmode;
930 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
932 tree field;
933 enum machine_mode mode = VOIDmode;
935 /* A record which has any BLKmode members must itself be BLKmode;
936 it can't go in a register.
937 Unless the member is BLKmode only because it isn't aligned. */
938 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
940 int bitpos;
942 if (TREE_CODE (field) != FIELD_DECL)
943 continue;
945 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
946 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
947 goto record_lose;
949 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
950 goto record_lose;
952 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
954 /* Must be BLKmode if any field crosses a word boundary,
955 since extract_bit_field can't handle that in registers. */
956 if (bitpos / BITS_PER_WORD
957 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
958 / BITS_PER_WORD)
959 /* But there is no problem if the field is entire words. */
960 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
961 goto record_lose;
963 /* If this field is the whole struct, remember its mode so
964 that, say, we can put a double in a class into a DF
965 register instead of forcing it to live in the stack. */
966 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
967 mode = DECL_MODE (field);
970 if (mode != VOIDmode)
971 /* We only have one real field; use its mode. */
972 TYPE_MODE (type) = mode;
973 else
974 TYPE_MODE (type)
975 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
976 MODE_INT, 1);
978 /* If structure's known alignment is less than
979 what the scalar mode would need, and it matters,
980 then stick with BLKmode. */
981 if (STRICT_ALIGNMENT
982 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
983 || ((int)TYPE_ALIGN (type)
984 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
986 if (TYPE_MODE (type) != BLKmode)
987 /* If this is the only reason this type is BLKmode,
988 then don't force containing types to be BLKmode. */
989 TYPE_NO_FORCE_BLK (type) = 1;
990 TYPE_MODE (type) = BLKmode;
993 record_lose: ;
996 /* Lay out any static members. This is done now
997 because their type may use the record's type. */
998 while (pending_statics)
1000 layout_decl (TREE_VALUE (pending_statics), 0);
1001 pending_statics = TREE_CHAIN (pending_statics);
1003 break;
1005 case UNION_TYPE:
1006 case QUAL_UNION_TYPE:
1007 layout_union (type);
1008 TYPE_MODE (type) = BLKmode;
1009 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1010 /* If structure's known alignment is less than
1011 what the scalar mode would need, and it matters,
1012 then stick with BLKmode. */
1013 && (! STRICT_ALIGNMENT
1014 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1015 || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1017 tree field;
1018 /* A union which has any BLKmode members must itself be BLKmode;
1019 it can't go in a register.
1020 Unless the member is BLKmode only because it isn't aligned. */
1021 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1023 if (TREE_CODE (field) != FIELD_DECL)
1024 continue;
1026 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1027 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1028 goto union_lose;
1031 TYPE_MODE (type)
1032 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1033 MODE_INT, 1);
1035 union_lose: ;
1037 break;
1039 case SET_TYPE: /* Used by Chill and Pascal. */
1040 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1041 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1042 abort();
1043 else
1045 #ifndef SET_WORD_SIZE
1046 #define SET_WORD_SIZE BITS_PER_WORD
1047 #endif
1048 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1049 int size_in_bits
1050 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1051 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1052 int rounded_size
1053 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1054 if (rounded_size > alignment)
1055 TYPE_MODE (type) = BLKmode;
1056 else
1057 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1058 TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1059 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1060 TYPE_ALIGN (type) = alignment;
1061 TYPE_PRECISION (type) = size_in_bits;
1063 break;
1065 case FILE_TYPE:
1066 /* The size may vary in different languages, so the language front end
1067 should fill in the size. */
1068 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1069 TYPE_MODE (type) = BLKmode;
1070 break;
1072 default:
1073 abort ();
1074 } /* end switch */
1076 /* Normally, use the alignment corresponding to the mode chosen.
1077 However, where strict alignment is not required, avoid
1078 over-aligning structures, since most compilers do not do this
1079 alignment. */
1081 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1082 && (STRICT_ALIGNMENT
1083 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1084 && TREE_CODE (type) != QUAL_UNION_TYPE
1085 && TREE_CODE (type) != ARRAY_TYPE)))
1086 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1088 /* Do machine-dependent extra alignment. */
1089 #ifdef ROUND_TYPE_ALIGN
1090 TYPE_ALIGN (type)
1091 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1092 #endif
1094 #ifdef ROUND_TYPE_SIZE
1095 if (TYPE_SIZE (type) != 0)
1096 TYPE_SIZE (type)
1097 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1098 #endif
1100 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1101 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1102 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1104 /* If we failed to find a simple way to calculate the unit size
1105 of the type above, find it by division. */
1106 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1108 TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1109 size_int (BITS_PER_UNIT));
1112 /* Once again evaluate only once, either now or as soon as safe. */
1113 if (TYPE_SIZE_UNIT (type) != 0
1114 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1115 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1117 /* Also layout any other variants of the type. */
1118 if (TYPE_NEXT_VARIANT (type)
1119 || type != TYPE_MAIN_VARIANT (type))
1121 tree variant;
1122 /* Record layout info of this variant. */
1123 tree size = TYPE_SIZE (type);
1124 tree size_unit = TYPE_SIZE_UNIT (type);
1125 int align = TYPE_ALIGN (type);
1126 enum machine_mode mode = TYPE_MODE (type);
1128 /* Copy it into all variants. */
1129 for (variant = TYPE_MAIN_VARIANT (type);
1130 variant;
1131 variant = TYPE_NEXT_VARIANT (variant))
1133 TYPE_SIZE (variant) = size;
1134 TYPE_SIZE_UNIT (variant) = size_unit;
1135 TYPE_ALIGN (variant) = align;
1136 TYPE_MODE (variant) = mode;
1140 pop_obstacks ();
1141 resume_momentary (old);
1144 /* Create and return a type for signed integers of PRECISION bits. */
1146 tree
1147 make_signed_type (precision)
1148 int precision;
1150 register tree type = make_node (INTEGER_TYPE);
1152 TYPE_PRECISION (type) = precision;
1154 /* Create the extreme values based on the number of bits. */
1156 TYPE_MIN_VALUE (type)
1157 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1158 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1159 (((HOST_WIDE_INT) (-1)
1160 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1161 ? precision - HOST_BITS_PER_WIDE_INT - 1
1162 : 0))));
1163 TYPE_MAX_VALUE (type)
1164 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1165 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1166 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1167 ? (((HOST_WIDE_INT) 1
1168 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1169 : 0));
1171 /* Give this type's extreme values this type as their type. */
1173 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1174 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1176 /* The first type made with this or `make_unsigned_type'
1177 is the type for size values. */
1179 if (sizetype == 0)
1180 set_sizetype (type);
1182 /* Lay out the type: set its alignment, size, etc. */
1184 layout_type (type);
1186 return type;
1189 /* Create and return a type for unsigned integers of PRECISION bits. */
1191 tree
1192 make_unsigned_type (precision)
1193 int precision;
1195 register tree type = make_node (INTEGER_TYPE);
1197 TYPE_PRECISION (type) = precision;
1199 /* The first type made with this or `make_signed_type'
1200 is the type for size values. */
1202 if (sizetype == 0)
1204 TREE_UNSIGNED (type) = 1;
1205 set_sizetype (type);
1208 fixup_unsigned_type (type);
1209 return type;
1212 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1213 Also update the type of any standard type's sizes made so far. */
1215 void
1216 set_sizetype (type)
1217 tree type;
1219 int oprecision = TYPE_PRECISION (type), precision;
1221 sizetype = type;
1223 /* The *bitsizetype types use a precision that avoids overflows when
1224 calculating signed sizes / offsets in bits.
1226 We are allocating bitsizetype once and change it in place when
1227 we decide later that we want to change it. This way, we avoid the
1228 hassle of changing all the TYPE_SIZE (TREE_TYPE (sometype))
1229 individually in each front end. */
1230 if (! bitsizetype)
1231 bitsizetype = make_node (INTEGER_TYPE);
1232 if (TYPE_NAME (sizetype) && ! TYPE_NAME (bitsizetype))
1233 TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1235 precision = oprecision + BITS_PER_UNIT_LOG + 1;
1236 /* However, when cross-compiling from a 32 bit to a 64 bit host,
1237 we are limited to 64 bit precision. */
1238 if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1239 precision = 2 * HOST_BITS_PER_WIDE_INT;
1240 TYPE_PRECISION (bitsizetype) = precision;
1241 if (TREE_UNSIGNED (type))
1242 fixup_unsigned_type (bitsizetype);
1243 else
1244 fixup_signed_type (bitsizetype);
1245 layout_type (bitsizetype);
1247 if (TREE_UNSIGNED (type))
1249 usizetype = sizetype;
1250 ubitsizetype = bitsizetype;
1251 ssizetype = make_signed_type (oprecision);
1252 sbitsizetype = make_signed_type (precision);
1254 else
1256 ssizetype = sizetype;
1257 sbitsizetype = bitsizetype;
1258 usizetype = make_unsigned_type (oprecision);
1259 ubitsizetype = make_unsigned_type (precision);
1263 /* Set the extreme values of TYPE based on its precision in bits,
1264 then lay it out. Used when make_signed_type won't do
1265 because the tree code is not INTEGER_TYPE.
1266 E.g. for Pascal, when the -fsigned-char option is given. */
1268 void
1269 fixup_signed_type (type)
1270 tree type;
1272 register int precision = TYPE_PRECISION (type);
1274 TYPE_MIN_VALUE (type)
1275 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1276 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1277 (((HOST_WIDE_INT) (-1)
1278 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1279 ? precision - HOST_BITS_PER_WIDE_INT - 1
1280 : 0))));
1281 TYPE_MAX_VALUE (type)
1282 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1283 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1284 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1285 ? (((HOST_WIDE_INT) 1
1286 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1287 : 0));
1289 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1290 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1292 /* Lay out the type: set its alignment, size, etc. */
1294 layout_type (type);
1297 /* Set the extreme values of TYPE based on its precision in bits,
1298 then lay it out. This is used both in `make_unsigned_type'
1299 and for enumeral types. */
1301 void
1302 fixup_unsigned_type (type)
1303 tree type;
1305 register int precision = TYPE_PRECISION (type);
1307 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1308 TYPE_MAX_VALUE (type)
1309 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1310 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1311 precision - HOST_BITS_PER_WIDE_INT > 0
1312 ? ((unsigned HOST_WIDE_INT) ~0
1313 >> (HOST_BITS_PER_WIDE_INT
1314 - (precision - HOST_BITS_PER_WIDE_INT)))
1315 : 0);
1316 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1317 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1319 /* Lay out the type: set its alignment, size, etc. */
1321 layout_type (type);
1324 /* Find the best machine mode to use when referencing a bit field of length
1325 BITSIZE bits starting at BITPOS.
1327 The underlying object is known to be aligned to a boundary of ALIGN bits.
1328 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1329 larger than LARGEST_MODE (usually SImode).
1331 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1332 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1333 mode meeting these conditions.
1335 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1336 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1337 all the conditions. */
1339 enum machine_mode
1340 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1341 int bitsize, bitpos;
1342 int align;
1343 enum machine_mode largest_mode;
1344 int volatilep;
1346 enum machine_mode mode;
1347 int unit = 0;
1349 /* Find the narrowest integer mode that contains the bit field. */
1350 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1351 mode = GET_MODE_WIDER_MODE (mode))
1353 unit = GET_MODE_BITSIZE (mode);
1354 if ((bitpos % unit) + bitsize <= unit)
1355 break;
1358 if (mode == MAX_MACHINE_MODE
1359 /* It is tempting to omit the following line
1360 if STRICT_ALIGNMENT is true.
1361 But that is incorrect, since if the bitfield uses part of 3 bytes
1362 and we use a 4-byte mode, we could get a spurious segv
1363 if the extra 4th byte is past the end of memory.
1364 (Though at least one Unix compiler ignores this problem:
1365 that on the Sequent 386 machine. */
1366 || MIN (unit, BIGGEST_ALIGNMENT) > align
1367 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1368 return VOIDmode;
1370 if (SLOW_BYTE_ACCESS && ! volatilep)
1372 enum machine_mode wide_mode = VOIDmode, tmode;
1374 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1375 tmode = GET_MODE_WIDER_MODE (tmode))
1377 unit = GET_MODE_BITSIZE (tmode);
1378 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1379 && unit <= BITS_PER_WORD
1380 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1381 && (largest_mode == VOIDmode
1382 || unit <= GET_MODE_BITSIZE (largest_mode)))
1383 wide_mode = tmode;
1386 if (wide_mode != VOIDmode)
1387 return wide_mode;
1390 return mode;
1393 /* Save all variables describing the current status into the structure *P.
1394 This is used before starting a nested function. */
1396 void
1397 save_storage_status (p)
1398 struct function *p ATTRIBUTE_UNUSED;
1400 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1401 p->pending_sizes = pending_sizes;
1402 p->immediate_size_expand = immediate_size_expand;
1403 #endif /* 0 */
1406 /* Restore all variables describing the current status from the structure *P.
1407 This is used after a nested function. */
1409 void
1410 restore_storage_status (p)
1411 struct function *p ATTRIBUTE_UNUSED;
1413 #if 0
1414 pending_sizes = p->pending_sizes;
1415 immediate_size_expand = p->immediate_size_expand;
1416 #endif /* 0 */