Do not generate error message about unrecognised command line switches of
[official-gcc.git] / gcc / stor-layout.c
blobf8a1e1dde63a5e9e94b221550124674c2731b2ec
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 in number of bits. */
265 DECL_SIZE (decl) = bitsize_int (spec_size, 0);
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) = bitsize_int (GET_MODE_BITSIZE (xmode), 0);
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) = bitsize_int (const_size, 0L);
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) = bitsize_int (const_size, 0L);
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 HOST_WIDE_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 tree dsize;
629 /* Enums which are local to this class need not be laid out. */
630 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
631 continue;
633 layout_decl (field, 0);
634 DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
636 /* Union must be at least as aligned as any field requires. */
638 union_align = MAX (union_align, DECL_ALIGN (field));
640 #ifdef PCC_BITFIELD_TYPE_MATTERS
641 /* On the m88000, a bit field of declare type `int'
642 forces the entire union to have `int' alignment. */
643 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
644 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
645 #endif
647 dsize = DECL_SIZE (field);
648 if (TREE_CODE (rec) == UNION_TYPE)
650 /* Set union_size to max (decl_size, union_size).
651 There are more and less general ways to do this.
652 Use only CONST_SIZE unless forced to use VAR_SIZE. */
654 if (TREE_CODE (dsize) == INTEGER_CST
655 && ! TREE_CONSTANT_OVERFLOW (dsize)
656 && TREE_INT_CST_HIGH (dsize) == 0)
657 const_size
658 = MAX (const_size, TREE_INT_CST_LOW (dsize));
659 else if (var_size == 0)
660 var_size = dsize;
661 else
662 var_size = size_binop (MAX_EXPR, var_size, dsize);
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 || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
944 continue;
946 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
947 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
948 goto record_lose;
950 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
951 goto record_lose;
953 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
955 /* Must be BLKmode if any field crosses a word boundary,
956 since extract_bit_field can't handle that in registers. */
957 if (bitpos / BITS_PER_WORD
958 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
959 / BITS_PER_WORD)
960 /* But there is no problem if the field is entire words. */
961 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
962 goto record_lose;
964 /* If this field is the whole struct, remember its mode so
965 that, say, we can put a double in a class into a DF
966 register instead of forcing it to live in the stack. */
967 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
968 mode = DECL_MODE (field);
971 if (mode != VOIDmode)
972 /* We only have one real field; use its mode. */
973 TYPE_MODE (type) = mode;
974 else
975 TYPE_MODE (type)
976 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
977 MODE_INT, 1);
979 /* If structure's known alignment is less than
980 what the scalar mode would need, and it matters,
981 then stick with BLKmode. */
982 if (STRICT_ALIGNMENT
983 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
984 || ((int)TYPE_ALIGN (type)
985 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
987 if (TYPE_MODE (type) != BLKmode)
988 /* If this is the only reason this type is BLKmode,
989 then don't force containing types to be BLKmode. */
990 TYPE_NO_FORCE_BLK (type) = 1;
991 TYPE_MODE (type) = BLKmode;
994 record_lose: ;
997 /* Lay out any static members. This is done now
998 because their type may use the record's type. */
999 while (pending_statics)
1001 layout_decl (TREE_VALUE (pending_statics), 0);
1002 pending_statics = TREE_CHAIN (pending_statics);
1004 break;
1006 case UNION_TYPE:
1007 case QUAL_UNION_TYPE:
1008 layout_union (type);
1009 TYPE_MODE (type) = BLKmode;
1010 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1011 /* If structure's known alignment is less than
1012 what the scalar mode would need, and it matters,
1013 then stick with BLKmode. */
1014 && (! STRICT_ALIGNMENT
1015 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1016 || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1018 tree field;
1019 /* A union which has any BLKmode members must itself be BLKmode;
1020 it can't go in a register.
1021 Unless the member is BLKmode only because it isn't aligned. */
1022 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1024 if (TREE_CODE (field) != FIELD_DECL)
1025 continue;
1027 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1028 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1029 goto union_lose;
1032 TYPE_MODE (type)
1033 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1034 MODE_INT, 1);
1036 union_lose: ;
1038 break;
1040 case SET_TYPE: /* Used by Chill and Pascal. */
1041 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1042 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1043 abort();
1044 else
1046 #ifndef SET_WORD_SIZE
1047 #define SET_WORD_SIZE BITS_PER_WORD
1048 #endif
1049 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1050 int size_in_bits
1051 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1052 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1053 int rounded_size
1054 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1055 if (rounded_size > alignment)
1056 TYPE_MODE (type) = BLKmode;
1057 else
1058 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1059 TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1060 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1061 TYPE_ALIGN (type) = alignment;
1062 TYPE_PRECISION (type) = size_in_bits;
1064 break;
1066 case FILE_TYPE:
1067 /* The size may vary in different languages, so the language front end
1068 should fill in the size. */
1069 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1070 TYPE_MODE (type) = BLKmode;
1071 break;
1073 default:
1074 abort ();
1075 } /* end switch */
1077 /* Normally, use the alignment corresponding to the mode chosen.
1078 However, where strict alignment is not required, avoid
1079 over-aligning structures, since most compilers do not do this
1080 alignment. */
1082 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1083 && (STRICT_ALIGNMENT
1084 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1085 && TREE_CODE (type) != QUAL_UNION_TYPE
1086 && TREE_CODE (type) != ARRAY_TYPE)))
1087 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1089 /* Do machine-dependent extra alignment. */
1090 #ifdef ROUND_TYPE_ALIGN
1091 TYPE_ALIGN (type)
1092 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1093 #endif
1095 #ifdef ROUND_TYPE_SIZE
1096 if (TYPE_SIZE (type) != 0)
1097 TYPE_SIZE (type)
1098 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1099 #endif
1101 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1102 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1103 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1105 /* If we failed to find a simple way to calculate the unit size
1106 of the type above, find it by division. */
1107 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1109 TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1110 size_int (BITS_PER_UNIT));
1113 /* Once again evaluate only once, either now or as soon as safe. */
1114 if (TYPE_SIZE_UNIT (type) != 0
1115 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1116 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1118 /* Also layout any other variants of the type. */
1119 if (TYPE_NEXT_VARIANT (type)
1120 || type != TYPE_MAIN_VARIANT (type))
1122 tree variant;
1123 /* Record layout info of this variant. */
1124 tree size = TYPE_SIZE (type);
1125 tree size_unit = TYPE_SIZE_UNIT (type);
1126 int align = TYPE_ALIGN (type);
1127 enum machine_mode mode = TYPE_MODE (type);
1129 /* Copy it into all variants. */
1130 for (variant = TYPE_MAIN_VARIANT (type);
1131 variant;
1132 variant = TYPE_NEXT_VARIANT (variant))
1134 TYPE_SIZE (variant) = size;
1135 TYPE_SIZE_UNIT (variant) = size_unit;
1136 TYPE_ALIGN (variant) = align;
1137 TYPE_MODE (variant) = mode;
1141 pop_obstacks ();
1142 resume_momentary (old);
1145 /* Create and return a type for signed integers of PRECISION bits. */
1147 tree
1148 make_signed_type (precision)
1149 int precision;
1151 register tree type = make_node (INTEGER_TYPE);
1153 TYPE_PRECISION (type) = precision;
1155 /* Create the extreme values based on the number of bits. */
1157 TYPE_MIN_VALUE (type)
1158 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1159 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1160 (((HOST_WIDE_INT) (-1)
1161 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1162 ? precision - HOST_BITS_PER_WIDE_INT - 1
1163 : 0))));
1164 TYPE_MAX_VALUE (type)
1165 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1166 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1167 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1168 ? (((HOST_WIDE_INT) 1
1169 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1170 : 0));
1172 /* Give this type's extreme values this type as their type. */
1174 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1175 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1177 /* The first type made with this or `make_unsigned_type'
1178 is the type for size values. */
1180 if (sizetype == 0)
1181 set_sizetype (type);
1183 /* Lay out the type: set its alignment, size, etc. */
1185 layout_type (type);
1187 return type;
1190 /* Create and return a type for unsigned integers of PRECISION bits. */
1192 tree
1193 make_unsigned_type (precision)
1194 int precision;
1196 register tree type = make_node (INTEGER_TYPE);
1198 TYPE_PRECISION (type) = precision;
1200 /* The first type made with this or `make_signed_type'
1201 is the type for size values. */
1203 if (sizetype == 0)
1205 TREE_UNSIGNED (type) = 1;
1206 set_sizetype (type);
1209 fixup_unsigned_type (type);
1210 return type;
1213 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1214 Also update the type of any standard type's sizes made so far. */
1216 void
1217 set_sizetype (type)
1218 tree type;
1220 int oprecision = TYPE_PRECISION (type), precision;
1222 sizetype = type;
1224 /* The *bitsizetype types use a precision that avoids overflows when
1225 calculating signed sizes / offsets in bits. */
1226 precision = oprecision + BITS_PER_UNIT_LOG + 1;
1227 /* However, when cross-compiling from a 32 bit to a 64 bit host,
1228 we are limited to 64 bit precision. */
1229 if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1230 precision = 2 * HOST_BITS_PER_WIDE_INT;
1232 bitsizetype = make_node (INTEGER_TYPE);
1233 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1234 TYPE_PRECISION (bitsizetype) = precision;
1235 if (TREE_UNSIGNED (type))
1236 fixup_unsigned_type (bitsizetype);
1237 else
1238 fixup_signed_type (bitsizetype);
1239 layout_type (bitsizetype);
1241 if (TREE_UNSIGNED (type))
1243 usizetype = sizetype;
1244 ubitsizetype = bitsizetype;
1245 ssizetype = make_signed_type (oprecision);
1246 sbitsizetype = make_signed_type (precision);
1248 else
1250 ssizetype = sizetype;
1251 sbitsizetype = bitsizetype;
1252 usizetype = make_unsigned_type (oprecision);
1253 ubitsizetype = make_unsigned_type (precision);
1255 TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1257 ggc_add_tree_root ((tree*) &sizetype_tab, sizeof(sizetype_tab)/sizeof(tree));
1260 /* Set the extreme values of TYPE based on its precision in bits,
1261 then lay it out. Used when make_signed_type won't do
1262 because the tree code is not INTEGER_TYPE.
1263 E.g. for Pascal, when the -fsigned-char option is given. */
1265 void
1266 fixup_signed_type (type)
1267 tree type;
1269 register int precision = TYPE_PRECISION (type);
1271 TYPE_MIN_VALUE (type)
1272 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1273 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1274 (((HOST_WIDE_INT) (-1)
1275 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1276 ? precision - HOST_BITS_PER_WIDE_INT - 1
1277 : 0))));
1278 TYPE_MAX_VALUE (type)
1279 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1280 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1281 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1282 ? (((HOST_WIDE_INT) 1
1283 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1284 : 0));
1286 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1287 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1289 /* Lay out the type: set its alignment, size, etc. */
1291 layout_type (type);
1294 /* Set the extreme values of TYPE based on its precision in bits,
1295 then lay it out. This is used both in `make_unsigned_type'
1296 and for enumeral types. */
1298 void
1299 fixup_unsigned_type (type)
1300 tree type;
1302 register int precision = TYPE_PRECISION (type);
1304 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1305 TYPE_MAX_VALUE (type)
1306 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1307 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1308 precision - HOST_BITS_PER_WIDE_INT > 0
1309 ? ((unsigned HOST_WIDE_INT) ~0
1310 >> (HOST_BITS_PER_WIDE_INT
1311 - (precision - HOST_BITS_PER_WIDE_INT)))
1312 : 0);
1313 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1314 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1316 /* Lay out the type: set its alignment, size, etc. */
1318 layout_type (type);
1321 /* Find the best machine mode to use when referencing a bit field of length
1322 BITSIZE bits starting at BITPOS.
1324 The underlying object is known to be aligned to a boundary of ALIGN bits.
1325 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1326 larger than LARGEST_MODE (usually SImode).
1328 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1329 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1330 mode meeting these conditions.
1332 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1333 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1334 all the conditions. */
1336 enum machine_mode
1337 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1338 int bitsize, bitpos;
1339 int align;
1340 enum machine_mode largest_mode;
1341 int volatilep;
1343 enum machine_mode mode;
1344 int unit = 0;
1346 /* Find the narrowest integer mode that contains the bit field. */
1347 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1348 mode = GET_MODE_WIDER_MODE (mode))
1350 unit = GET_MODE_BITSIZE (mode);
1351 if ((bitpos % unit) + bitsize <= unit)
1352 break;
1355 if (mode == VOIDmode
1356 /* It is tempting to omit the following line
1357 if STRICT_ALIGNMENT is true.
1358 But that is incorrect, since if the bitfield uses part of 3 bytes
1359 and we use a 4-byte mode, we could get a spurious segv
1360 if the extra 4th byte is past the end of memory.
1361 (Though at least one Unix compiler ignores this problem:
1362 that on the Sequent 386 machine. */
1363 || MIN (unit, BIGGEST_ALIGNMENT) > align
1364 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1365 return VOIDmode;
1367 if (SLOW_BYTE_ACCESS && ! volatilep)
1369 enum machine_mode wide_mode = VOIDmode, tmode;
1371 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1372 tmode = GET_MODE_WIDER_MODE (tmode))
1374 unit = GET_MODE_BITSIZE (tmode);
1375 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1376 && unit <= BITS_PER_WORD
1377 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1378 && (largest_mode == VOIDmode
1379 || unit <= GET_MODE_BITSIZE (largest_mode)))
1380 wide_mode = tmode;
1383 if (wide_mode != VOIDmode)
1384 return wide_mode;
1387 return mode;
1390 /* This function is run once to initialize stor-layout.c. */
1392 void
1393 init_stor_layout_once ()
1395 ggc_add_tree_root (&pending_sizes, 1);