* optabs.c (init_optabs): Initialize fixtab, fixtrunctab, floattab,
[official-gcc.git] / gcc / stor-layout.c
blob43ae6f2cf472278016cebefebc189960154e4235
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #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 /* Set to one when set_sizetype has been called. */
35 static int sizetype_set;
37 /* List of types created before set_sizetype has been called. We do not
38 make this a GGC root since we want these nodes to be reclaimed. */
39 static tree early_type_list;
41 /* Data type for the expressions representing sizes of data types.
42 It is the first integer type laid out. */
43 tree sizetype_tab[(int) TYPE_KIND_LAST];
45 /* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
47 unsigned int maximum_field_alignment;
49 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
50 May be overridden by front-ends. */
51 unsigned int set_alignment = 0;
53 static void finalize_record_size PARAMS ((record_layout_info));
54 static void finalize_type_size PARAMS ((tree));
55 static void place_union_field PARAMS ((record_layout_info, tree));
56 extern void debug_rli PARAMS ((record_layout_info));
58 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
60 static tree pending_sizes;
62 /* Nonzero means cannot safely call expand_expr now,
63 so put variable sizes onto `pending_sizes' instead. */
65 int immediate_size_expand;
67 /* Get a list of all the objects put on the pending sizes list. */
69 tree
70 get_pending_sizes ()
72 tree chain = pending_sizes;
73 tree t;
75 /* Put each SAVE_EXPR into the current function. */
76 for (t = chain; t; t = TREE_CHAIN (t))
77 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
79 pending_sizes = 0;
80 return chain;
83 /* Put a chain of objects into the pending sizes list, which must be
84 empty. */
86 void
87 put_pending_sizes (chain)
88 tree chain;
90 if (pending_sizes)
91 abort ();
93 pending_sizes = chain;
96 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
97 to serve as the actual size-expression for a type or decl. */
99 tree
100 variable_size (size)
101 tree size;
103 /* If the language-processor is to take responsibility for variable-sized
104 items (e.g., languages which have elaboration procedures like Ada),
105 just return SIZE unchanged. Likewise for self-referential sizes. */
106 if (TREE_CONSTANT (size)
107 || global_bindings_p () < 0 || contains_placeholder_p (size))
108 return size;
110 size = save_expr (size);
112 /* If an array with a variable number of elements is declared, and
113 the elements require destruction, we will emit a cleanup for the
114 array. That cleanup is run both on normal exit from the block
115 and in the exception-handler for the block. Normally, when code
116 is used in both ordinary code and in an exception handler it is
117 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
118 not wish to do that here; the array-size is the same in both
119 places. */
120 if (TREE_CODE (size) == SAVE_EXPR)
121 SAVE_EXPR_PERSISTENT_P (size) = 1;
123 if (global_bindings_p ())
125 if (TREE_CONSTANT (size))
126 error ("type size can't be explicitly evaluated");
127 else
128 error ("variable-size type declared outside of any function");
130 return size_one_node;
133 if (immediate_size_expand)
134 /* NULL_RTX is not defined; neither is the rtx type.
135 Also, we would like to pass const0_rtx here, but don't have it. */
136 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
137 VOIDmode, 0);
138 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
139 /* The front-end doesn't want us to keep a list of the expressions
140 that determine sizes for variable size objects. */
142 else
143 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
145 return size;
148 #ifndef MAX_FIXED_MODE_SIZE
149 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
150 #endif
152 /* Return the machine mode to use for a nonscalar of SIZE bits.
153 The mode must be in class CLASS, and have exactly that many bits.
154 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
155 be used. */
157 enum machine_mode
158 mode_for_size (size, class, limit)
159 unsigned int size;
160 enum mode_class class;
161 int limit;
163 register enum machine_mode mode;
165 if (limit && size > MAX_FIXED_MODE_SIZE)
166 return BLKmode;
168 /* Get the first mode which has this size, in the specified class. */
169 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
170 mode = GET_MODE_WIDER_MODE (mode))
171 if (GET_MODE_BITSIZE (mode) == size)
172 return mode;
174 return BLKmode;
177 /* Similar, except passed a tree node. */
179 enum machine_mode
180 mode_for_size_tree (size, class, limit)
181 tree size;
182 enum mode_class class;
183 int limit;
185 if (TREE_CODE (size) != INTEGER_CST
186 /* What we really want to say here is that the size can fit in a
187 host integer, but we know there's no way we'd find a mode for
188 this many bits, so there's no point in doing the precise test. */
189 || compare_tree_int (size, 1000) > 0)
190 return BLKmode;
191 else
192 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
195 /* Similar, but never return BLKmode; return the narrowest mode that
196 contains at least the requested number of bits. */
198 enum machine_mode
199 smallest_mode_for_size (size, class)
200 unsigned int size;
201 enum mode_class class;
203 register enum machine_mode mode;
205 /* Get the first mode which has at least this size, in the
206 specified class. */
207 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
208 mode = GET_MODE_WIDER_MODE (mode))
209 if (GET_MODE_BITSIZE (mode) >= size)
210 return mode;
212 abort ();
215 /* Find an integer mode of the exact same size, or BLKmode on failure. */
217 enum machine_mode
218 int_mode_for_mode (mode)
219 enum machine_mode mode;
221 switch (GET_MODE_CLASS (mode))
223 case MODE_INT:
224 case MODE_PARTIAL_INT:
225 break;
227 case MODE_COMPLEX_INT:
228 case MODE_COMPLEX_FLOAT:
229 case MODE_FLOAT:
230 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
231 break;
233 case MODE_RANDOM:
234 if (mode == BLKmode)
235 break;
237 /* ... fall through ... */
239 case MODE_CC:
240 default:
241 abort ();
244 return mode;
247 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
248 This can only be applied to objects of a sizetype. */
250 tree
251 round_up (value, divisor)
252 tree value;
253 int divisor;
255 tree arg = size_int_type (divisor, TREE_TYPE (value));
257 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
260 /* Likewise, but round down. */
262 tree
263 round_down (value, divisor)
264 tree value;
265 int divisor;
267 tree arg = size_int_type (divisor, TREE_TYPE (value));
269 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
272 /* Set the size, mode and alignment of a ..._DECL node.
273 TYPE_DECL does need this for C++.
274 Note that LABEL_DECL and CONST_DECL nodes do not need this,
275 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
276 Don't call layout_decl for them.
278 KNOWN_ALIGN is the amount of alignment we can assume this
279 decl has with no special effort. It is relevant only for FIELD_DECLs
280 and depends on the previous fields.
281 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
282 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
283 the record will be aligned to suit. */
285 void
286 layout_decl (decl, known_align)
287 tree decl;
288 unsigned int known_align;
290 register tree type = TREE_TYPE (decl);
291 register enum tree_code code = TREE_CODE (decl);
293 if (code == CONST_DECL)
294 return;
295 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
296 && code != TYPE_DECL && code != FIELD_DECL)
297 abort ();
299 if (type == error_mark_node)
300 type = void_type_node;
302 /* Usually the size and mode come from the data type without change,
303 however, the front-end may set the explicit width of the field, so its
304 size may not be the same as the size of its type. This happens with
305 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
306 also happens with other fields. For example, the C++ front-end creates
307 zero-sized fields corresponding to empty base classes, and depends on
308 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
309 size in bytes from the size in bits. If we have already set the mode,
310 don't set it again since we can be called twice for FIELD_DECLs. */
312 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
313 if (DECL_MODE (decl) == VOIDmode)
314 DECL_MODE (decl) = TYPE_MODE (type);
316 if (DECL_SIZE (decl) == 0)
318 DECL_SIZE (decl) = TYPE_SIZE (type);
319 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
321 else
322 DECL_SIZE_UNIT (decl)
323 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
324 bitsize_unit_node));
326 /* Force alignment required for the data type.
327 But if the decl itself wants greater alignment, don't override that.
328 Likewise, if the decl is packed, don't override it. */
329 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
330 && (DECL_ALIGN (decl) == 0
331 || (! (code == FIELD_DECL && DECL_PACKED (decl))
332 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
334 DECL_ALIGN (decl) = TYPE_ALIGN (type);
335 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
338 /* For fields, set the bit field type and update the alignment. */
339 if (code == FIELD_DECL)
341 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
342 if (maximum_field_alignment != 0)
343 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
344 else if (DECL_PACKED (decl))
346 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
347 DECL_USER_ALIGN (decl) = 0;
351 /* See if we can use an ordinary integer mode for a bit-field.
352 Conditions are: a fixed size that is correct for another mode
353 and occupying a complete byte or bytes on proper boundary. */
354 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
355 && TYPE_SIZE (type) != 0
356 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
357 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
359 register enum machine_mode xmode
360 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
362 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
364 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
365 DECL_ALIGN (decl));
366 DECL_MODE (decl) = xmode;
367 DECL_BIT_FIELD (decl) = 0;
371 /* Turn off DECL_BIT_FIELD if we won't need it set. */
372 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
373 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
374 && known_align >= TYPE_ALIGN (type)
375 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
376 && DECL_SIZE_UNIT (decl) != 0)
377 DECL_BIT_FIELD (decl) = 0;
379 /* Evaluate nonconstant size only once, either now or as soon as safe. */
380 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
381 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
382 if (DECL_SIZE_UNIT (decl) != 0
383 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
384 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
386 /* If requested, warn about definitions of large data objects. */
387 if (warn_larger_than
388 && (code == VAR_DECL || code == PARM_DECL)
389 && ! DECL_EXTERNAL (decl))
391 tree size = DECL_SIZE_UNIT (decl);
393 if (size != 0 && TREE_CODE (size) == INTEGER_CST
394 && compare_tree_int (size, larger_than_size) > 0)
396 unsigned int size_as_int = TREE_INT_CST_LOW (size);
398 if (compare_tree_int (size, size_as_int) == 0)
399 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
400 else
401 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
402 larger_than_size);
407 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
408 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
409 is to be passed to all other layout functions for this record. It is the
410 responsibility of the caller to call `free' for the storage returned.
411 Note that garbage collection is not permitted until we finish laying
412 out the record. */
414 record_layout_info
415 start_record_layout (t)
416 tree t;
418 record_layout_info rli
419 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
421 rli->t = t;
423 /* If the type has a minimum specified alignment (via an attribute
424 declaration, for example) use it -- otherwise, start with a
425 one-byte alignment. */
426 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
427 rli->unpacked_align = rli->record_align;
428 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
430 #ifdef STRUCTURE_SIZE_BOUNDARY
431 /* Packed structures don't need to have minimum size. */
432 if (! TYPE_PACKED (t))
433 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
434 #endif
436 rli->offset = size_zero_node;
437 rli->bitpos = bitsize_zero_node;
438 rli->pending_statics = 0;
439 rli->packed_maybe_necessary = 0;
441 return rli;
444 /* These four routines perform computations that convert between
445 the offset/bitpos forms and byte and bit offsets. */
447 tree
448 bit_from_pos (offset, bitpos)
449 tree offset, bitpos;
451 return size_binop (PLUS_EXPR, bitpos,
452 size_binop (MULT_EXPR, convert (bitsizetype, offset),
453 bitsize_unit_node));
456 tree
457 byte_from_pos (offset, bitpos)
458 tree offset, bitpos;
460 return size_binop (PLUS_EXPR, offset,
461 convert (sizetype,
462 size_binop (TRUNC_DIV_EXPR, bitpos,
463 bitsize_unit_node)));
466 void
467 pos_from_byte (poffset, pbitpos, off_align, pos)
468 tree *poffset, *pbitpos;
469 unsigned int off_align;
470 tree pos;
472 *poffset
473 = size_binop (MULT_EXPR,
474 convert (sizetype,
475 size_binop (FLOOR_DIV_EXPR, pos,
476 bitsize_int (off_align
477 / BITS_PER_UNIT))),
478 size_int (off_align / BITS_PER_UNIT));
479 *pbitpos = size_binop (MULT_EXPR,
480 size_binop (FLOOR_MOD_EXPR, pos,
481 bitsize_int (off_align / BITS_PER_UNIT)),
482 bitsize_unit_node);
485 void
486 pos_from_bit (poffset, pbitpos, off_align, pos)
487 tree *poffset, *pbitpos;
488 unsigned int off_align;
489 tree pos;
491 *poffset = size_binop (MULT_EXPR,
492 convert (sizetype,
493 size_binop (FLOOR_DIV_EXPR, pos,
494 bitsize_int (off_align))),
495 size_int (off_align / BITS_PER_UNIT));
496 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
499 /* Given a pointer to bit and byte offsets and an offset alignment,
500 normalize the offsets so they are within the alignment. */
502 void
503 normalize_offset (poffset, pbitpos, off_align)
504 tree *poffset, *pbitpos;
505 unsigned int off_align;
507 /* If the bit position is now larger than it should be, adjust it
508 downwards. */
509 if (compare_tree_int (*pbitpos, off_align) >= 0)
511 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
512 bitsize_int (off_align));
514 *poffset
515 = size_binop (PLUS_EXPR, *poffset,
516 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
517 size_int (off_align / BITS_PER_UNIT)));
519 *pbitpos
520 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
524 /* Print debugging information about the information in RLI. */
526 void
527 debug_rli (rli)
528 record_layout_info rli;
530 print_node_brief (stderr, "type", rli->t, 0);
531 print_node_brief (stderr, "\noffset", rli->offset, 0);
532 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
534 fprintf (stderr, "\nrec_align = %u, unpack_align = %u, off_align = %u\n",
535 rli->record_align, rli->unpacked_align, rli->offset_align);
536 if (rli->packed_maybe_necessary)
537 fprintf (stderr, "packed may be necessary\n");
539 if (rli->pending_statics)
541 fprintf (stderr, "pending statics:\n");
542 debug_tree (rli->pending_statics);
546 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
547 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
549 void
550 normalize_rli (rli)
551 record_layout_info rli;
553 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
556 /* Returns the size in bytes allocated so far. */
558 tree
559 rli_size_unit_so_far (rli)
560 record_layout_info rli;
562 return byte_from_pos (rli->offset, rli->bitpos);
565 /* Returns the size in bits allocated so far. */
567 tree
568 rli_size_so_far (rli)
569 record_layout_info rli;
571 return bit_from_pos (rli->offset, rli->bitpos);
574 /* Called from place_field to handle unions. */
576 static void
577 place_union_field (rli, field)
578 record_layout_info rli;
579 tree field;
581 unsigned int desired_align;
583 layout_decl (field, 0);
585 DECL_FIELD_OFFSET (field) = size_zero_node;
586 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
587 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
589 desired_align = DECL_ALIGN (field);
591 #ifdef BIGGEST_FIELD_ALIGNMENT
592 /* Some targets (i.e. i386) limit union field alignment
593 to a lower boundary than alignment of variables unless
594 it was overridden by attribute aligned. */
595 if (! DECL_USER_ALIGN (field))
596 desired_align =
597 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
598 #endif
600 /* Union must be at least as aligned as any field requires. */
601 rli->record_align = MAX (rli->record_align, desired_align);
603 #ifdef PCC_BITFIELD_TYPE_MATTERS
604 /* On the m88000, a bit field of declare type `int' forces the
605 entire union to have `int' alignment. */
606 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
607 rli->record_align = MAX (rli->record_align,
608 TYPE_ALIGN (TREE_TYPE (field)));
609 #endif
611 /* We assume the union's size will be a multiple of a byte so we don't
612 bother with BITPOS. */
613 if (TREE_CODE (rli->t) == UNION_TYPE)
614 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
615 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
616 rli->offset = fold (build (COND_EXPR, sizetype,
617 DECL_QUALIFIER (field),
618 DECL_SIZE_UNIT (field), rli->offset));
621 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
622 is a FIELD_DECL to be added after those fields already present in
623 T. (FIELD is not actually added to the TYPE_FIELDS list here;
624 callers that desire that behavior must manually perform that step.) */
626 void
627 place_field (rli, field)
628 record_layout_info rli;
629 tree field;
631 /* The alignment required for FIELD. */
632 unsigned int desired_align;
633 /* The alignment FIELD would have if we just dropped it into the
634 record as it presently stands. */
635 unsigned int known_align;
636 unsigned int actual_align;
637 unsigned int user_align;
638 /* The type of this field. */
639 tree type = TREE_TYPE (field);
641 /* If FIELD is static, then treat it like a separate variable, not
642 really like a structure field. If it is a FUNCTION_DECL, it's a
643 method. In both cases, all we do is lay out the decl, and we do
644 it *after* the record is laid out. */
645 if (TREE_CODE (field) == VAR_DECL)
647 rli->pending_statics = tree_cons (NULL_TREE, field,
648 rli->pending_statics);
649 return;
652 /* Enumerators and enum types which are local to this class need not
653 be laid out. Likewise for initialized constant fields. */
654 else if (TREE_CODE (field) != FIELD_DECL)
655 return;
657 /* Unions are laid out very differently than records, so split
658 that code off to another function. */
659 else if (TREE_CODE (rli->t) != RECORD_TYPE)
661 place_union_field (rli, field);
662 return;
665 /* Work out the known alignment so far. Note that A & (-A) is the
666 value of the least-significant bit in A that is one. */
667 if (! integer_zerop (rli->bitpos))
668 known_align = (tree_low_cst (rli->bitpos, 1)
669 & - tree_low_cst (rli->bitpos, 1));
670 else if (integer_zerop (rli->offset))
671 known_align = BIGGEST_ALIGNMENT;
672 else if (host_integerp (rli->offset, 1))
673 known_align = (BITS_PER_UNIT
674 * (tree_low_cst (rli->offset, 1)
675 & - tree_low_cst (rli->offset, 1)));
676 else
677 known_align = rli->offset_align;
679 /* Lay out the field so we know what alignment it needs. For a
680 packed field, use the alignment as specified, disregarding what
681 the type would want. */
682 desired_align = DECL_ALIGN (field);
683 user_align = DECL_USER_ALIGN (field);
684 layout_decl (field, known_align);
685 if (! DECL_PACKED (field))
687 desired_align = DECL_ALIGN (field);
688 user_align = DECL_USER_ALIGN (field);
691 #ifdef BIGGEST_FIELD_ALIGNMENT
692 /* Some targets (i.e. i386, VMS) limit struct field alignment
693 to a lower boundary than alignment of variables unless
694 it was overridden by attribute aligned. */
695 if (! user_align)
696 desired_align =
697 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
698 #endif
699 #ifdef ADJUST_FIELD_ALIGN
700 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
701 #endif
703 /* Record must have at least as much alignment as any field.
704 Otherwise, the alignment of the field within the record is
705 meaningless. */
706 #ifdef PCC_BITFIELD_TYPE_MATTERS
707 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
708 && DECL_BIT_FIELD_TYPE (field)
709 && ! integer_zerop (TYPE_SIZE (type)))
711 /* For these machines, a zero-length field does not
712 affect the alignment of the structure as a whole.
713 It does, however, affect the alignment of the next field
714 within the structure. */
715 if (! integer_zerop (DECL_SIZE (field)))
716 rli->record_align = MAX (rli->record_align, desired_align);
717 else if (! DECL_PACKED (field))
718 desired_align = TYPE_ALIGN (type);
720 /* A named bit field of declared type `int'
721 forces the entire structure to have `int' alignment. */
722 if (DECL_NAME (field) != 0)
724 unsigned int type_align = TYPE_ALIGN (type);
726 if (maximum_field_alignment != 0)
727 type_align = MIN (type_align, maximum_field_alignment);
728 else if (DECL_PACKED (field))
729 type_align = MIN (type_align, BITS_PER_UNIT);
731 rli->record_align = MAX (rli->record_align, type_align);
732 if (warn_packed)
733 rli->unpacked_align = MAX (rli->unpacked_align,
734 TYPE_ALIGN (type));
737 else
738 #endif
740 rli->record_align = MAX (rli->record_align, desired_align);
741 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
744 if (warn_packed && DECL_PACKED (field))
746 if (known_align > TYPE_ALIGN (type))
748 if (TYPE_ALIGN (type) > desired_align)
750 if (STRICT_ALIGNMENT)
751 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
752 else
753 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
756 else
757 rli->packed_maybe_necessary = 1;
760 /* Does this field automatically have alignment it needs by virtue
761 of the fields that precede it and the record's own alignment? */
762 if (known_align < desired_align)
764 /* No, we need to skip space before this field.
765 Bump the cumulative size to multiple of field alignment. */
767 if (warn_padded)
768 warning_with_decl (field, "padding struct to align `%s'");
770 /* If the alignment is still within offset_align, just align
771 the bit position. */
772 if (desired_align < rli->offset_align)
773 rli->bitpos = round_up (rli->bitpos, desired_align);
774 else
776 /* First adjust OFFSET by the partial bits, then align. */
777 rli->offset
778 = size_binop (PLUS_EXPR, rli->offset,
779 convert (sizetype,
780 size_binop (CEIL_DIV_EXPR, rli->bitpos,
781 bitsize_unit_node)));
782 rli->bitpos = bitsize_zero_node;
784 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
787 if (! TREE_CONSTANT (rli->offset))
788 rli->offset_align = desired_align;
792 /* Handle compatibility with PCC. Note that if the record has any
793 variable-sized fields, we need not worry about compatibility. */
794 #ifdef PCC_BITFIELD_TYPE_MATTERS
795 if (PCC_BITFIELD_TYPE_MATTERS
796 && TREE_CODE (field) == FIELD_DECL
797 && type != error_mark_node
798 && DECL_BIT_FIELD (field)
799 && ! DECL_PACKED (field)
800 && maximum_field_alignment == 0
801 && ! integer_zerop (DECL_SIZE (field))
802 && host_integerp (DECL_SIZE (field), 1)
803 && host_integerp (rli->offset, 1)
804 && host_integerp (TYPE_SIZE (type), 1))
806 unsigned int type_align = TYPE_ALIGN (type);
807 tree dsize = DECL_SIZE (field);
808 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
809 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
810 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
812 /* A bit field may not span more units of alignment of its type
813 than its type itself. Advance to next boundary if necessary. */
814 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
815 type_align - 1)
816 / type_align)
817 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
818 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
819 rli->bitpos = round_up (rli->bitpos, type_align);
821 #endif
823 #ifdef BITFIELD_NBYTES_LIMITED
824 if (BITFIELD_NBYTES_LIMITED
825 && TREE_CODE (field) == FIELD_DECL
826 && type != error_mark_node
827 && DECL_BIT_FIELD_TYPE (field)
828 && ! DECL_PACKED (field)
829 && ! integer_zerop (DECL_SIZE (field))
830 && host_integerp (DECL_SIZE (field), 1)
831 && host_integerp (rli->offset, 1)
832 && host_integerp (TYPE_SIZE (type), 1))
834 unsigned int type_align = TYPE_ALIGN (type);
835 tree dsize = DECL_SIZE (field);
836 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
837 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
838 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
840 if (maximum_field_alignment != 0)
841 type_align = MIN (type_align, maximum_field_alignment);
842 /* ??? This test is opposite the test in the containing if
843 statement, so this code is unreachable currently. */
844 else if (DECL_PACKED (field))
845 type_align = MIN (type_align, BITS_PER_UNIT);
847 /* A bit field may not span the unit of alignment of its type.
848 Advance to next boundary if necessary. */
849 /* ??? This code should match the code above for the
850 PCC_BITFIELD_TYPE_MATTERS case. */
851 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
852 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
853 / type_align))
854 rli->bitpos = round_up (rli->bitpos, type_align);
856 #endif
858 /* Offset so far becomes the position of this field after normalizing. */
859 normalize_rli (rli);
860 DECL_FIELD_OFFSET (field) = rli->offset;
861 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
862 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
864 /* If this field ended up more aligned than we thought it would be (we
865 approximate this by seeing if its position changed), lay out the field
866 again; perhaps we can use an integral mode for it now. */
867 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
868 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
869 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
870 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
871 actual_align = BIGGEST_ALIGNMENT;
872 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
873 actual_align = (BITS_PER_UNIT
874 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
875 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
876 else
877 actual_align = DECL_OFFSET_ALIGN (field);
879 if (known_align != actual_align)
880 layout_decl (field, actual_align);
882 /* Now add size of this field to the size of the record. If the size is
883 not constant, treat the field as being a multiple of bytes and just
884 adjust the offset, resetting the bit position. Otherwise, apportion the
885 size amongst the bit position and offset. First handle the case of an
886 unspecified size, which can happen when we have an invalid nested struct
887 definition, such as struct j { struct j { int i; } }. The error message
888 is printed in finish_struct. */
889 if (DECL_SIZE (field) == 0)
890 /* Do nothing. */;
891 else if (! TREE_CONSTANT (DECL_SIZE_UNIT (field)))
893 rli->offset
894 = size_binop (PLUS_EXPR, rli->offset,
895 convert (sizetype,
896 size_binop (CEIL_DIV_EXPR, rli->bitpos,
897 bitsize_unit_node)));
898 rli->offset
899 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
900 rli->bitpos = bitsize_zero_node;
901 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
903 else
905 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
906 normalize_rli (rli);
910 /* Assuming that all the fields have been laid out, this function uses
911 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
912 inidicated by RLI. */
914 static void
915 finalize_record_size (rli)
916 record_layout_info rli;
918 tree unpadded_size, unpadded_size_unit;
920 /* Now we want just byte and bit offsets, so set the offset alignment
921 to be a byte and then normalize. */
922 rli->offset_align = BITS_PER_UNIT;
923 normalize_rli (rli);
925 /* Determine the desired alignment. */
926 #ifdef ROUND_TYPE_ALIGN
927 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
928 rli->record_align);
929 #else
930 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
931 #endif
932 TYPE_USER_ALIGN (rli->t) = 1;
934 /* Compute the size so far. Be sure to allow for extra bits in the
935 size in bytes. We have guaranteed above that it will be no more
936 than a single byte. */
937 unpadded_size = rli_size_so_far (rli);
938 unpadded_size_unit = rli_size_unit_so_far (rli);
939 if (! integer_zerop (rli->bitpos))
940 unpadded_size_unit
941 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
943 /* Record the un-rounded size in the binfo node. But first we check
944 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
945 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
947 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
948 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
951 /* Round the size up to be a multiple of the required alignment */
952 #ifdef ROUND_TYPE_SIZE
953 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
954 TYPE_ALIGN (rli->t));
955 TYPE_SIZE_UNIT (rli->t)
956 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
957 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
958 #else
959 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
960 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
961 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
962 #endif
964 if (warn_padded && TREE_CONSTANT (unpadded_size)
965 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
966 warning ("padding struct size to alignment boundary");
968 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
969 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
970 && TREE_CONSTANT (unpadded_size))
972 tree unpacked_size;
974 #ifdef ROUND_TYPE_ALIGN
975 rli->unpacked_align
976 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
977 #else
978 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
979 #endif
981 #ifdef ROUND_TYPE_SIZE
982 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
983 rli->unpacked_align);
984 #else
985 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
986 #endif
988 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
990 TYPE_PACKED (rli->t) = 0;
992 if (TYPE_NAME (rli->t))
994 char *name;
996 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
997 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
998 else
999 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1001 if (STRICT_ALIGNMENT)
1002 warning ("packed attribute causes inefficient alignment for `%s'", name);
1003 else
1004 warning ("packed attribute is unnecessary for `%s'", name);
1006 else
1008 if (STRICT_ALIGNMENT)
1009 warning ("packed attribute causes inefficient alignment");
1010 else
1011 warning ("packed attribute is unnecessary");
1017 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1019 void
1020 compute_record_mode (type)
1021 tree type;
1023 tree field;
1024 enum machine_mode mode = VOIDmode;
1026 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1027 However, if possible, we use a mode that fits in a register
1028 instead, in order to allow for better optimization down the
1029 line. */
1030 TYPE_MODE (type) = BLKmode;
1032 if (! host_integerp (TYPE_SIZE (type), 1))
1033 return;
1035 /* A record which has any BLKmode members must itself be
1036 BLKmode; it can't go in a register. Unless the member is
1037 BLKmode only because it isn't aligned. */
1038 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1040 unsigned HOST_WIDE_INT bitpos;
1042 if (TREE_CODE (field) != FIELD_DECL)
1043 continue;
1045 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1046 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1047 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1048 || ! host_integerp (bit_position (field), 1)
1049 || ! host_integerp (DECL_SIZE (field), 1))
1050 return;
1052 bitpos = int_bit_position (field);
1054 /* Must be BLKmode if any field crosses a word boundary,
1055 since extract_bit_field can't handle that in registers. */
1056 if (bitpos / BITS_PER_WORD
1057 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
1058 / BITS_PER_WORD)
1059 /* But there is no problem if the field is entire words. */
1060 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1061 return;
1063 /* If this field is the whole struct, remember its mode so
1064 that, say, we can put a double in a class into a DF
1065 register instead of forcing it to live in the stack. However,
1066 we don't support using such a mode if there is no integer mode
1067 of the same size, so don't set it here. */
1068 if (field == TYPE_FIELDS (type) && TREE_CHAIN (field) == 0
1069 && int_mode_for_mode (DECL_MODE (field)) != BLKmode)
1070 mode = DECL_MODE (field);
1072 #ifdef STRUCT_FORCE_BLK
1073 /* With some targets, eg. c4x, it is sub-optimal
1074 to access an aligned BLKmode structure as a scalar. */
1075 if (mode == VOIDmode && STRUCT_FORCE_BLK (field))
1076 return;
1077 #endif /* STRUCT_FORCE_BLK */
1080 if (mode != VOIDmode)
1081 /* We only have one real field; use its mode. */
1082 TYPE_MODE (type) = mode;
1083 else
1084 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1086 /* If structure's known alignment is less than what the scalar
1087 mode would need, and it matters, then stick with BLKmode. */
1088 if (TYPE_MODE (type) != BLKmode
1089 && STRICT_ALIGNMENT
1090 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1091 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1093 /* If this is the only reason this type is BLKmode, then
1094 don't force containing types to be BLKmode. */
1095 TYPE_NO_FORCE_BLK (type) = 1;
1096 TYPE_MODE (type) = BLKmode;
1100 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1101 out. */
1103 static void
1104 finalize_type_size (type)
1105 tree type;
1107 /* Normally, use the alignment corresponding to the mode chosen.
1108 However, where strict alignment is not required, avoid
1109 over-aligning structures, since most compilers do not do this
1110 alignment. */
1112 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1113 && (STRICT_ALIGNMENT
1114 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1115 && TREE_CODE (type) != QUAL_UNION_TYPE
1116 && TREE_CODE (type) != ARRAY_TYPE)))
1118 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1119 TYPE_USER_ALIGN (type) = 0;
1122 /* Do machine-dependent extra alignment. */
1123 #ifdef ROUND_TYPE_ALIGN
1124 TYPE_ALIGN (type)
1125 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1126 #endif
1128 /* If we failed to find a simple way to calculate the unit size
1129 of the type, find it by division. */
1130 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1131 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1132 result will fit in sizetype. We will get more efficient code using
1133 sizetype, so we force a conversion. */
1134 TYPE_SIZE_UNIT (type)
1135 = convert (sizetype,
1136 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1137 bitsize_unit_node));
1139 if (TYPE_SIZE (type) != 0)
1141 #ifdef ROUND_TYPE_SIZE
1142 TYPE_SIZE (type)
1143 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1144 TYPE_SIZE_UNIT (type)
1145 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1146 TYPE_ALIGN (type) / BITS_PER_UNIT);
1147 #else
1148 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1149 TYPE_SIZE_UNIT (type)
1150 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1151 #endif
1154 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1155 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1156 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1157 if (TYPE_SIZE_UNIT (type) != 0
1158 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1159 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1161 /* Also layout any other variants of the type. */
1162 if (TYPE_NEXT_VARIANT (type)
1163 || type != TYPE_MAIN_VARIANT (type))
1165 tree variant;
1166 /* Record layout info of this variant. */
1167 tree size = TYPE_SIZE (type);
1168 tree size_unit = TYPE_SIZE_UNIT (type);
1169 unsigned int align = TYPE_ALIGN (type);
1170 unsigned int user_align = TYPE_USER_ALIGN (type);
1171 enum machine_mode mode = TYPE_MODE (type);
1173 /* Copy it into all variants. */
1174 for (variant = TYPE_MAIN_VARIANT (type);
1175 variant != 0;
1176 variant = TYPE_NEXT_VARIANT (variant))
1178 TYPE_SIZE (variant) = size;
1179 TYPE_SIZE_UNIT (variant) = size_unit;
1180 TYPE_ALIGN (variant) = align;
1181 TYPE_USER_ALIGN (variant) = user_align;
1182 TYPE_MODE (variant) = mode;
1187 /* Do all of the work required to layout the type indicated by RLI,
1188 once the fields have been laid out. This function will call `free'
1189 for RLI. */
1191 void
1192 finish_record_layout (rli)
1193 record_layout_info rli;
1195 /* Compute the final size. */
1196 finalize_record_size (rli);
1198 /* Compute the TYPE_MODE for the record. */
1199 compute_record_mode (rli->t);
1201 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1202 finalize_type_size (rli->t);
1204 /* Lay out any static members. This is done now because their type
1205 may use the record's type. */
1206 while (rli->pending_statics)
1208 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1209 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1212 /* Clean up. */
1213 free (rli);
1216 /* Calculate the mode, size, and alignment for TYPE.
1217 For an array type, calculate the element separation as well.
1218 Record TYPE on the chain of permanent or temporary types
1219 so that dbxout will find out about it.
1221 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1222 layout_type does nothing on such a type.
1224 If the type is incomplete, its TYPE_SIZE remains zero. */
1226 void
1227 layout_type (type)
1228 tree type;
1230 int old;
1232 if (type == 0)
1233 abort ();
1235 /* Do nothing if type has been laid out before. */
1236 if (TYPE_SIZE (type))
1237 return;
1239 /* Make sure all nodes we allocate are not momentary; they must last
1240 past the current statement. */
1241 old = suspend_momentary ();
1243 /* Put all our nodes into the same obstack as the type. Also,
1244 make expressions saveable (this is a no-op for permanent types). */
1246 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
1247 saveable_allocation ();
1249 switch (TREE_CODE (type))
1251 case LANG_TYPE:
1252 /* This kind of type is the responsibility
1253 of the language-specific code. */
1254 abort ();
1256 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1257 if (TYPE_PRECISION (type) == 0)
1258 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1260 /* ... fall through ... */
1262 case INTEGER_TYPE:
1263 case ENUMERAL_TYPE:
1264 case CHAR_TYPE:
1265 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1266 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1267 TREE_UNSIGNED (type) = 1;
1269 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1270 MODE_INT);
1271 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1272 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1273 break;
1275 case REAL_TYPE:
1276 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1277 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1278 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1279 break;
1281 case COMPLEX_TYPE:
1282 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1283 TYPE_MODE (type)
1284 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1285 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1286 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1288 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1289 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1290 break;
1292 case VECTOR_TYPE:
1294 tree subtype;
1296 subtype = TREE_TYPE (type);
1297 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1298 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1299 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1301 break;
1303 case VOID_TYPE:
1304 /* This is an incomplete type and so doesn't have a size. */
1305 TYPE_ALIGN (type) = 1;
1306 TYPE_USER_ALIGN (type) = 0;
1307 TYPE_MODE (type) = VOIDmode;
1308 break;
1310 case OFFSET_TYPE:
1311 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1312 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1313 TYPE_MODE (type) = ptr_mode;
1314 break;
1316 case FUNCTION_TYPE:
1317 case METHOD_TYPE:
1318 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1319 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1320 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1321 break;
1323 case POINTER_TYPE:
1324 case REFERENCE_TYPE:
1325 TYPE_MODE (type) = ptr_mode;
1326 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1327 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1328 TREE_UNSIGNED (type) = 1;
1329 TYPE_PRECISION (type) = POINTER_SIZE;
1330 break;
1332 case ARRAY_TYPE:
1334 register tree index = TYPE_DOMAIN (type);
1335 register tree element = TREE_TYPE (type);
1337 build_pointer_type (element);
1339 /* We need to know both bounds in order to compute the size. */
1340 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1341 && TYPE_SIZE (element))
1343 tree ub = TYPE_MAX_VALUE (index);
1344 tree lb = TYPE_MIN_VALUE (index);
1345 tree length;
1346 tree element_size;
1348 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
1349 test for negative below covers it. */
1350 if (TREE_CODE (ub) == MAX_EXPR
1351 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
1352 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
1353 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
1354 lb, 0))
1355 ub = TREE_OPERAND (ub, 1);
1356 else if (TREE_CODE (ub) == MAX_EXPR
1357 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
1358 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
1359 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
1361 lb, 0))
1362 ub = TREE_OPERAND (ub, 0);
1364 /* The initial subtraction should happen in the original type so
1365 that (possible) negative values are handled appropriately. */
1366 length = size_binop (PLUS_EXPR, size_one_node,
1367 convert (sizetype,
1368 fold (build (MINUS_EXPR,
1369 TREE_TYPE (lb),
1370 ub, lb))));
1372 /* If neither bound is a constant and sizetype is signed, make
1373 sure the size is never negative. We should really do this
1374 if *either* bound is non-constant, but this is the best
1375 compromise between C and Ada. */
1376 if (! TREE_UNSIGNED (sizetype)
1377 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1378 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1379 length = size_binop (MAX_EXPR, length, size_zero_node);
1381 /* Special handling for arrays of bits (for Chill). */
1382 element_size = TYPE_SIZE (element);
1383 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
1385 HOST_WIDE_INT maxvalue
1386 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
1387 HOST_WIDE_INT minvalue
1388 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
1390 if (maxvalue - minvalue == 1
1391 && (maxvalue == 1 || maxvalue == 0))
1392 element_size = integer_one_node;
1395 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1396 convert (bitsizetype, length));
1398 /* If we know the size of the element, calculate the total
1399 size directly, rather than do some division thing below.
1400 This optimization helps Fortran assumed-size arrays
1401 (where the size of the array is determined at runtime)
1402 substantially.
1403 Note that we can't do this in the case where the size of
1404 the elements is one bit since TYPE_SIZE_UNIT cannot be
1405 set correctly in that case. */
1406 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1407 TYPE_SIZE_UNIT (type)
1408 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1411 /* Now round the alignment and size,
1412 using machine-dependent criteria if any. */
1414 #ifdef ROUND_TYPE_ALIGN
1415 TYPE_ALIGN (type)
1416 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1417 #else
1418 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1419 #endif
1421 #ifdef ROUND_TYPE_SIZE
1422 if (TYPE_SIZE (type) != 0)
1424 tree tmp
1425 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1427 /* If the rounding changed the size of the type, remove any
1428 pre-calculated TYPE_SIZE_UNIT. */
1429 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1430 TYPE_SIZE_UNIT (type) = NULL;
1432 TYPE_SIZE (type) = tmp;
1434 #endif
1436 TYPE_MODE (type) = BLKmode;
1437 if (TYPE_SIZE (type) != 0
1438 /* BLKmode elements force BLKmode aggregate;
1439 else extract/store fields may lose. */
1440 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1441 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1443 TYPE_MODE (type)
1444 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1446 if (TYPE_MODE (type) != BLKmode
1447 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1448 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1449 && TYPE_MODE (type) != BLKmode)
1451 TYPE_NO_FORCE_BLK (type) = 1;
1452 TYPE_MODE (type) = BLKmode;
1455 break;
1458 case RECORD_TYPE:
1459 case UNION_TYPE:
1460 case QUAL_UNION_TYPE:
1462 tree field;
1463 record_layout_info rli;
1465 /* Initialize the layout information. */
1466 rli = start_record_layout (type);
1468 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1469 in the reverse order in building the COND_EXPR that denotes
1470 its size. We reverse them again later. */
1471 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1472 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1474 /* Place all the fields. */
1475 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1476 place_field (rli, field);
1478 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1479 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1481 /* Finish laying out the record. */
1482 finish_record_layout (rli);
1484 break;
1486 case SET_TYPE: /* Used by Chill and Pascal. */
1487 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1488 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1489 abort();
1490 else
1492 #ifndef SET_WORD_SIZE
1493 #define SET_WORD_SIZE BITS_PER_WORD
1494 #endif
1495 unsigned int alignment
1496 = set_alignment ? set_alignment : SET_WORD_SIZE;
1497 int size_in_bits
1498 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1499 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1500 int rounded_size
1501 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1503 if (rounded_size > (int) alignment)
1504 TYPE_MODE (type) = BLKmode;
1505 else
1506 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1508 TYPE_SIZE (type) = bitsize_int (rounded_size);
1509 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1510 TYPE_ALIGN (type) = alignment;
1511 TYPE_USER_ALIGN (type) = 0;
1512 TYPE_PRECISION (type) = size_in_bits;
1514 break;
1516 case FILE_TYPE:
1517 /* The size may vary in different languages, so the language front end
1518 should fill in the size. */
1519 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1520 TYPE_USER_ALIGN (type) = 0;
1521 TYPE_MODE (type) = BLKmode;
1522 break;
1524 default:
1525 abort ();
1528 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1529 records and unions, finish_record_layout already called this
1530 function. */
1531 if (TREE_CODE (type) != RECORD_TYPE
1532 && TREE_CODE (type) != UNION_TYPE
1533 && TREE_CODE (type) != QUAL_UNION_TYPE)
1534 finalize_type_size (type);
1536 pop_obstacks ();
1537 resume_momentary (old);
1539 /* If this type is created before sizetype has been permanently set,
1540 record it so set_sizetype can fix it up. */
1541 if (! sizetype_set)
1542 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1545 /* Create and return a type for signed integers of PRECISION bits. */
1547 tree
1548 make_signed_type (precision)
1549 int precision;
1551 register tree type = make_node (INTEGER_TYPE);
1553 TYPE_PRECISION (type) = precision;
1555 fixup_signed_type (type);
1556 return type;
1559 /* Create and return a type for unsigned integers of PRECISION bits. */
1561 tree
1562 make_unsigned_type (precision)
1563 int precision;
1565 register tree type = make_node (INTEGER_TYPE);
1567 TYPE_PRECISION (type) = precision;
1569 fixup_unsigned_type (type);
1570 return type;
1573 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1574 value to enable integer types to be created. */
1576 void
1577 initialize_sizetypes ()
1579 tree t = make_node (INTEGER_TYPE);
1581 /* Set this so we do something reasonable for the build_int_2 calls
1582 below. */
1583 integer_type_node = t;
1585 TYPE_MODE (t) = SImode;
1586 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1587 TYPE_USER_ALIGN (t) = 0;
1588 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1589 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1590 TREE_UNSIGNED (t) = 1;
1591 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1592 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1593 TYPE_IS_SIZETYPE (t) = 1;
1595 /* 1000 avoids problems with possible overflow and is certainly
1596 larger than any size value we'd want to be storing. */
1597 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1599 /* These two must be different nodes because of the caching done in
1600 size_int_wide. */
1601 sizetype = t;
1602 bitsizetype = copy_node (t);
1603 integer_type_node = 0;
1606 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1607 Also update the type of any standard type's sizes made so far. */
1609 void
1610 set_sizetype (type)
1611 tree type;
1613 int oprecision = TYPE_PRECISION (type);
1614 /* The *bitsizetype types use a precision that avoids overflows when
1615 calculating signed sizes / offsets in bits. However, when
1616 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1617 precision. */
1618 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1619 2 * HOST_BITS_PER_WIDE_INT);
1620 unsigned int i;
1621 tree t;
1623 if (sizetype_set)
1624 abort ();
1626 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1627 sizetype = copy_node (type);
1628 TYPE_DOMAIN (sizetype) = type;
1629 TYPE_IS_SIZETYPE (sizetype) = 1;
1630 bitsizetype = make_node (INTEGER_TYPE);
1631 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1632 TYPE_PRECISION (bitsizetype) = precision;
1633 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1635 if (TREE_UNSIGNED (type))
1636 fixup_unsigned_type (bitsizetype);
1637 else
1638 fixup_signed_type (bitsizetype);
1640 layout_type (bitsizetype);
1642 if (TREE_UNSIGNED (type))
1644 usizetype = sizetype;
1645 ubitsizetype = bitsizetype;
1646 ssizetype = copy_node (make_signed_type (oprecision));
1647 sbitsizetype = copy_node (make_signed_type (precision));
1649 else
1651 ssizetype = sizetype;
1652 sbitsizetype = bitsizetype;
1653 usizetype = copy_node (make_unsigned_type (oprecision));
1654 ubitsizetype = copy_node (make_unsigned_type (precision));
1657 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1659 /* Show is a sizetype, is a main type, and has no pointers to it. */
1660 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1662 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1663 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1664 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1665 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1666 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1669 ggc_add_tree_root ((tree *) &sizetype_tab,
1670 sizeof sizetype_tab / sizeof (tree));
1672 /* Go down each of the types we already made and set the proper type
1673 for the sizes in them. */
1674 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1676 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1677 abort ();
1679 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1680 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1683 early_type_list = 0;
1684 sizetype_set = 1;
1687 /* Set the extreme values of TYPE based on its precision in bits,
1688 then lay it out. Used when make_signed_type won't do
1689 because the tree code is not INTEGER_TYPE.
1690 E.g. for Pascal, when the -fsigned-char option is given. */
1692 void
1693 fixup_signed_type (type)
1694 tree type;
1696 register int precision = TYPE_PRECISION (type);
1698 TYPE_MIN_VALUE (type)
1699 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1700 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1701 (((HOST_WIDE_INT) (-1)
1702 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1703 ? precision - HOST_BITS_PER_WIDE_INT - 1
1704 : 0))));
1705 TYPE_MAX_VALUE (type)
1706 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1707 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1708 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1709 ? (((HOST_WIDE_INT) 1
1710 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1711 : 0));
1713 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1714 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1716 /* Lay out the type: set its alignment, size, etc. */
1717 layout_type (type);
1720 /* Set the extreme values of TYPE based on its precision in bits,
1721 then lay it out. This is used both in `make_unsigned_type'
1722 and for enumeral types. */
1724 void
1725 fixup_unsigned_type (type)
1726 tree type;
1728 register int precision = TYPE_PRECISION (type);
1730 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1731 TYPE_MAX_VALUE (type)
1732 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1733 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1734 precision - HOST_BITS_PER_WIDE_INT > 0
1735 ? ((unsigned HOST_WIDE_INT) ~0
1736 >> (HOST_BITS_PER_WIDE_INT
1737 - (precision - HOST_BITS_PER_WIDE_INT)))
1738 : 0);
1739 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1740 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1742 /* Lay out the type: set its alignment, size, etc. */
1743 layout_type (type);
1746 /* Find the best machine mode to use when referencing a bit field of length
1747 BITSIZE bits starting at BITPOS.
1749 The underlying object is known to be aligned to a boundary of ALIGN bits.
1750 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1751 larger than LARGEST_MODE (usually SImode).
1753 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1754 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1755 mode meeting these conditions.
1757 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1758 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1759 all the conditions. */
1761 enum machine_mode
1762 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1763 int bitsize, bitpos;
1764 unsigned int align;
1765 enum machine_mode largest_mode;
1766 int volatilep;
1768 enum machine_mode mode;
1769 unsigned int unit = 0;
1771 /* Find the narrowest integer mode that contains the bit field. */
1772 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1773 mode = GET_MODE_WIDER_MODE (mode))
1775 unit = GET_MODE_BITSIZE (mode);
1776 if ((bitpos % unit) + bitsize <= unit)
1777 break;
1780 if (mode == VOIDmode
1781 /* It is tempting to omit the following line
1782 if STRICT_ALIGNMENT is true.
1783 But that is incorrect, since if the bitfield uses part of 3 bytes
1784 and we use a 4-byte mode, we could get a spurious segv
1785 if the extra 4th byte is past the end of memory.
1786 (Though at least one Unix compiler ignores this problem:
1787 that on the Sequent 386 machine. */
1788 || MIN (unit, BIGGEST_ALIGNMENT) > align
1789 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1790 return VOIDmode;
1792 if (SLOW_BYTE_ACCESS && ! volatilep)
1794 enum machine_mode wide_mode = VOIDmode, tmode;
1796 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1797 tmode = GET_MODE_WIDER_MODE (tmode))
1799 unit = GET_MODE_BITSIZE (tmode);
1800 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1801 && unit <= BITS_PER_WORD
1802 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1803 && (largest_mode == VOIDmode
1804 || unit <= GET_MODE_BITSIZE (largest_mode)))
1805 wide_mode = tmode;
1808 if (wide_mode != VOIDmode)
1809 return wide_mode;
1812 return mode;
1815 /* Return the alignment of MODE. This will be bounded by 1 and
1816 BIGGEST_ALIGNMENT. */
1818 unsigned int
1819 get_mode_alignment (mode)
1820 enum machine_mode mode;
1822 unsigned int alignment = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
1824 /* Extract the LSB of the size. */
1825 alignment = alignment & -alignment;
1827 alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1828 return alignment;
1831 /* This function is run once to initialize stor-layout.c. */
1833 void
1834 init_stor_layout_once ()
1836 ggc_add_tree_root (&pending_sizes, 1);