OSDN Git Service

2010-10-08 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / stor-layout.c
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, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "function.h"
32 #include "expr.h"
33 #include "output.h"
34 #include "diagnostic-core.h"
35 #include "toplev.h"
36 #include "ggc.h"
37 #include "target.h"
38 #include "langhooks.h"
39 #include "regs.h"
40 #include "params.h"
41 #include "cgraph.h"
42 #include "tree-inline.h"
43 #include "tree-dump.h"
44 #include "gimple.h"
45
46 /* Data type for the expressions representing sizes of data types.
47    It is the first integer type laid out.  */
48 tree sizetype_tab[(int) TYPE_KIND_LAST];
49
50 /* If nonzero, this is an upper limit on alignment of structure fields.
51    The value is measured in bits.  */
52 unsigned int maximum_field_alignment = TARGET_DEFAULT_PACK_STRUCT * BITS_PER_UNIT;
53 /* ... and its original value in bytes, specified via -fpack-struct=<value>.  */
54 unsigned int initial_max_fld_align = TARGET_DEFAULT_PACK_STRUCT;
55
56 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be allocated
57    in the address spaces' address_mode, not pointer_mode.   Set only by
58    internal_reference_types called only by a front end.  */
59 static int reference_types_internal = 0;
60
61 static tree self_referential_size (tree);
62 static void finalize_record_size (record_layout_info);
63 static void finalize_type_size (tree);
64 static void place_union_field (record_layout_info, tree);
65 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
66 static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
67                              HOST_WIDE_INT, tree);
68 #endif
69 extern void debug_rli (record_layout_info);
70 \f
71 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
72
73 static GTY(()) VEC(tree,gc) *pending_sizes;
74
75 /* Show that REFERENCE_TYPES are internal and should use address_mode.
76    Called only by front end.  */
77
78 void
79 internal_reference_types (void)
80 {
81   reference_types_internal = 1;
82 }
83
84 /* Get a VEC of all the objects put on the pending sizes list.  */
85
86 VEC(tree,gc) *
87 get_pending_sizes (void)
88 {
89   VEC(tree,gc) *chain = pending_sizes;
90
91   pending_sizes = 0;
92   return chain;
93 }
94
95 /* Add EXPR to the pending sizes list.  */
96
97 void
98 put_pending_size (tree expr)
99 {
100   /* Strip any simple arithmetic from EXPR to see if it has an underlying
101      SAVE_EXPR.  */
102   expr = skip_simple_arithmetic (expr);
103
104   if (TREE_CODE (expr) == SAVE_EXPR)
105     VEC_safe_push (tree, gc, pending_sizes, expr);
106 }
107
108 /* Put a chain of objects into the pending sizes list, which must be
109    empty.  */
110
111 void
112 put_pending_sizes (VEC(tree,gc) *chain)
113 {
114   gcc_assert (!pending_sizes);
115   pending_sizes = chain;
116 }
117
118 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
119    to serve as the actual size-expression for a type or decl.  */
120
121 tree
122 variable_size (tree size)
123 {
124   tree save;
125
126   /* Obviously.  */
127   if (TREE_CONSTANT (size))
128     return size;
129
130   /* If the size is self-referential, we can't make a SAVE_EXPR (see
131      save_expr for the rationale).  But we can do something else.  */
132   if (CONTAINS_PLACEHOLDER_P (size))
133     return self_referential_size (size);
134
135   /* If the language-processor is to take responsibility for variable-sized
136      items (e.g., languages which have elaboration procedures like Ada),
137      just return SIZE unchanged.  */
138   if (lang_hooks.decls.global_bindings_p () < 0)
139     return size;
140
141   size = save_expr (size);
142
143   /* If an array with a variable number of elements is declared, and
144      the elements require destruction, we will emit a cleanup for the
145      array.  That cleanup is run both on normal exit from the block
146      and in the exception-handler for the block.  Normally, when code
147      is used in both ordinary code and in an exception handler it is
148      `unsaved', i.e., all SAVE_EXPRs are recalculated.  However, we do
149      not wish to do that here; the array-size is the same in both
150      places.  */
151   save = skip_simple_arithmetic (size);
152
153   if (cfun && cfun->dont_save_pending_sizes_p)
154     /* The front-end doesn't want us to keep a list of the expressions
155        that determine sizes for variable size objects.  Trust it.  */
156     return size;
157
158   if (lang_hooks.decls.global_bindings_p ())
159     {
160       if (TREE_CONSTANT (size))
161         error ("type size can%'t be explicitly evaluated");
162       else
163         error ("variable-size type declared outside of any function");
164
165       return size_one_node;
166     }
167
168   put_pending_size (save);
169
170   return size;
171 }
172
173 /* An array of functions used for self-referential size computation.  */
174 static GTY(()) VEC (tree, gc) *size_functions;
175
176 /* Similar to copy_tree_r but do not copy component references involving
177    PLACEHOLDER_EXPRs.  These nodes are spotted in find_placeholder_in_expr
178    and substituted in substitute_in_expr.  */
179
180 static tree
181 copy_self_referential_tree_r (tree *tp, int *walk_subtrees, void *data)
182 {
183   enum tree_code code = TREE_CODE (*tp);
184
185   /* Stop at types, decls, constants like copy_tree_r.  */
186   if (TREE_CODE_CLASS (code) == tcc_type
187       || TREE_CODE_CLASS (code) == tcc_declaration
188       || TREE_CODE_CLASS (code) == tcc_constant)
189     {
190       *walk_subtrees = 0;
191       return NULL_TREE;
192     }
193
194   /* This is the pattern built in ada/make_aligning_type.  */
195   else if (code == ADDR_EXPR
196            && TREE_CODE (TREE_OPERAND (*tp, 0)) == PLACEHOLDER_EXPR)
197     {
198       *walk_subtrees = 0;
199       return NULL_TREE;
200     }
201
202   /* Default case: the component reference.  */
203   else if (code == COMPONENT_REF)
204     {
205       tree inner;
206       for (inner = TREE_OPERAND (*tp, 0);
207            REFERENCE_CLASS_P (inner);
208            inner = TREE_OPERAND (inner, 0))
209         ;
210
211       if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
212         {
213           *walk_subtrees = 0;
214           return NULL_TREE;
215         }
216     }
217
218   /* We're not supposed to have them in self-referential size trees
219      because we wouldn't properly control when they are evaluated.
220      However, not creating superfluous SAVE_EXPRs requires accurate
221      tracking of readonly-ness all the way down to here, which we
222      cannot always guarantee in practice.  So punt in this case.  */
223   else if (code == SAVE_EXPR)
224     return error_mark_node;
225
226   return copy_tree_r (tp, walk_subtrees, data);
227 }
228
229 /* Given a SIZE expression that is self-referential, return an equivalent
230    expression to serve as the actual size expression for a type.  */
231
232 static tree
233 self_referential_size (tree size)
234 {
235   static unsigned HOST_WIDE_INT fnno = 0;
236   VEC (tree, heap) *self_refs = NULL;
237   tree param_type_list = NULL, param_decl_list = NULL;
238   tree t, ref, return_type, fntype, fnname, fndecl;
239   unsigned int i;
240   char buf[128];
241   VEC(tree,gc) *args = NULL;
242
243   /* Do not factor out simple operations.  */
244   t = skip_simple_arithmetic (size);
245   if (TREE_CODE (t) == CALL_EXPR)
246     return size;
247
248   /* Collect the list of self-references in the expression.  */
249   find_placeholder_in_expr (size, &self_refs);
250   gcc_assert (VEC_length (tree, self_refs) > 0);
251
252   /* Obtain a private copy of the expression.  */
253   t = size;
254   if (walk_tree (&t, copy_self_referential_tree_r, NULL, NULL) != NULL_TREE)
255     return size;
256   size = t;
257
258   /* Build the parameter and argument lists in parallel; also
259      substitute the former for the latter in the expression.  */
260   args = VEC_alloc (tree, gc, VEC_length (tree, self_refs));
261   FOR_EACH_VEC_ELT (tree, self_refs, i, ref)
262     {
263       tree subst, param_name, param_type, param_decl;
264
265       if (DECL_P (ref))
266         {
267           /* We shouldn't have true variables here.  */
268           gcc_assert (TREE_READONLY (ref));
269           subst = ref;
270         }
271       /* This is the pattern built in ada/make_aligning_type.  */
272       else if (TREE_CODE (ref) == ADDR_EXPR)
273         subst = ref;
274       /* Default case: the component reference.  */
275       else
276         subst = TREE_OPERAND (ref, 1);
277
278       sprintf (buf, "p%d", i);
279       param_name = get_identifier (buf);
280       param_type = TREE_TYPE (ref);
281       param_decl
282         = build_decl (input_location, PARM_DECL, param_name, param_type);
283       if (targetm.calls.promote_prototypes (NULL_TREE)
284           && INTEGRAL_TYPE_P (param_type)
285           && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
286         DECL_ARG_TYPE (param_decl) = integer_type_node;
287       else
288         DECL_ARG_TYPE (param_decl) = param_type;
289       DECL_ARTIFICIAL (param_decl) = 1;
290       TREE_READONLY (param_decl) = 1;
291
292       size = substitute_in_expr (size, subst, param_decl);
293
294       param_type_list = tree_cons (NULL_TREE, param_type, param_type_list);
295       param_decl_list = chainon (param_decl, param_decl_list);
296       VEC_quick_push (tree, args, ref);
297     }
298
299   VEC_free (tree, heap, self_refs);
300
301   /* Append 'void' to indicate that the number of parameters is fixed.  */
302   param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
303
304   /* The 3 lists have been created in reverse order.  */
305   param_type_list = nreverse (param_type_list);
306   param_decl_list = nreverse (param_decl_list);
307
308   /* Build the function type.  */
309   return_type = TREE_TYPE (size);
310   fntype = build_function_type (return_type, param_type_list);
311
312   /* Build the function declaration.  */
313   sprintf (buf, "SZ"HOST_WIDE_INT_PRINT_UNSIGNED, fnno++);
314   fnname = get_file_function_name (buf);
315   fndecl = build_decl (input_location, FUNCTION_DECL, fnname, fntype);
316   for (t = param_decl_list; t; t = DECL_CHAIN (t))
317     DECL_CONTEXT (t) = fndecl;
318   DECL_ARGUMENTS (fndecl) = param_decl_list;
319   DECL_RESULT (fndecl)
320     = build_decl (input_location, RESULT_DECL, 0, return_type);
321   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
322
323   /* The function has been created by the compiler and we don't
324      want to emit debug info for it.  */
325   DECL_ARTIFICIAL (fndecl) = 1;
326   DECL_IGNORED_P (fndecl) = 1;
327
328   /* It is supposed to be "const" and never throw.  */
329   TREE_READONLY (fndecl) = 1;
330   TREE_NOTHROW (fndecl) = 1;
331
332   /* We want it to be inlined when this is deemed profitable, as
333      well as discarded if every call has been integrated.  */
334   DECL_DECLARED_INLINE_P (fndecl) = 1;
335
336   /* It is made up of a unique return statement.  */
337   DECL_INITIAL (fndecl) = make_node (BLOCK);
338   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
339   t = build2 (MODIFY_EXPR, return_type, DECL_RESULT (fndecl), size);
340   DECL_SAVED_TREE (fndecl) = build1 (RETURN_EXPR, void_type_node, t);
341   TREE_STATIC (fndecl) = 1;
342
343   /* Put it onto the list of size functions.  */
344   VEC_safe_push (tree, gc, size_functions, fndecl);
345
346   /* Replace the original expression with a call to the size function.  */
347   return build_call_expr_loc_vec (input_location, fndecl, args);
348 }
349
350 /* Take, queue and compile all the size functions.  It is essential that
351    the size functions be gimplified at the very end of the compilation
352    in order to guarantee transparent handling of self-referential sizes.
353    Otherwise the GENERIC inliner would not be able to inline them back
354    at each of their call sites, thus creating artificial non-constant
355    size expressions which would trigger nasty problems later on.  */
356
357 void
358 finalize_size_functions (void)
359 {
360   unsigned int i;
361   tree fndecl;
362
363   for (i = 0; VEC_iterate(tree, size_functions, i, fndecl); i++)
364     {
365       dump_function (TDI_original, fndecl);
366       gimplify_function_tree (fndecl);
367       dump_function (TDI_generic, fndecl);
368       cgraph_finalize_function (fndecl, false);
369     }
370
371   VEC_free (tree, gc, size_functions);
372 }
373 \f
374 /* Return the machine mode to use for a nonscalar of SIZE bits.  The
375    mode must be in class MCLASS, and have exactly that many value bits;
376    it may have padding as well.  If LIMIT is nonzero, modes of wider
377    than MAX_FIXED_MODE_SIZE will not be used.  */
378
379 enum machine_mode
380 mode_for_size (unsigned int size, enum mode_class mclass, int limit)
381 {
382   enum machine_mode mode;
383
384   if (limit && size > MAX_FIXED_MODE_SIZE)
385     return BLKmode;
386
387   /* Get the first mode which has this size, in the specified class.  */
388   for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
389        mode = GET_MODE_WIDER_MODE (mode))
390     if (GET_MODE_PRECISION (mode) == size)
391       return mode;
392
393   return BLKmode;
394 }
395
396 /* Similar, except passed a tree node.  */
397
398 enum machine_mode
399 mode_for_size_tree (const_tree size, enum mode_class mclass, int limit)
400 {
401   unsigned HOST_WIDE_INT uhwi;
402   unsigned int ui;
403
404   if (!host_integerp (size, 1))
405     return BLKmode;
406   uhwi = tree_low_cst (size, 1);
407   ui = uhwi;
408   if (uhwi != ui)
409     return BLKmode;
410   return mode_for_size (ui, mclass, limit);
411 }
412
413 /* Similar, but never return BLKmode; return the narrowest mode that
414    contains at least the requested number of value bits.  */
415
416 enum machine_mode
417 smallest_mode_for_size (unsigned int size, enum mode_class mclass)
418 {
419   enum machine_mode mode;
420
421   /* Get the first mode which has at least this size, in the
422      specified class.  */
423   for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
424        mode = GET_MODE_WIDER_MODE (mode))
425     if (GET_MODE_PRECISION (mode) >= size)
426       return mode;
427
428   gcc_unreachable ();
429 }
430
431 /* Find an integer mode of the exact same size, or BLKmode on failure.  */
432
433 enum machine_mode
434 int_mode_for_mode (enum machine_mode mode)
435 {
436   switch (GET_MODE_CLASS (mode))
437     {
438     case MODE_INT:
439     case MODE_PARTIAL_INT:
440       break;
441
442     case MODE_COMPLEX_INT:
443     case MODE_COMPLEX_FLOAT:
444     case MODE_FLOAT:
445     case MODE_DECIMAL_FLOAT:
446     case MODE_VECTOR_INT:
447     case MODE_VECTOR_FLOAT:
448     case MODE_FRACT:
449     case MODE_ACCUM:
450     case MODE_UFRACT:
451     case MODE_UACCUM:
452     case MODE_VECTOR_FRACT:
453     case MODE_VECTOR_ACCUM:
454     case MODE_VECTOR_UFRACT:
455     case MODE_VECTOR_UACCUM:
456       mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
457       break;
458
459     case MODE_RANDOM:
460       if (mode == BLKmode)
461         break;
462
463       /* ... fall through ...  */
464
465     case MODE_CC:
466     default:
467       gcc_unreachable ();
468     }
469
470   return mode;
471 }
472
473 /* Find a mode that is suitable for representing a vector with
474    NUNITS elements of mode INNERMODE.  Returns BLKmode if there
475    is no suitable mode.  */
476
477 enum machine_mode
478 mode_for_vector (enum machine_mode innermode, unsigned nunits)
479 {
480   enum machine_mode mode;
481
482   /* First, look for a supported vector type.  */
483   if (SCALAR_FLOAT_MODE_P (innermode))
484     mode = MIN_MODE_VECTOR_FLOAT;
485   else if (SCALAR_FRACT_MODE_P (innermode))
486     mode = MIN_MODE_VECTOR_FRACT;
487   else if (SCALAR_UFRACT_MODE_P (innermode))
488     mode = MIN_MODE_VECTOR_UFRACT;
489   else if (SCALAR_ACCUM_MODE_P (innermode))
490     mode = MIN_MODE_VECTOR_ACCUM;
491   else if (SCALAR_UACCUM_MODE_P (innermode))
492     mode = MIN_MODE_VECTOR_UACCUM;
493   else
494     mode = MIN_MODE_VECTOR_INT;
495
496   /* Do not check vector_mode_supported_p here.  We'll do that
497      later in vector_type_mode.  */
498   for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
499     if (GET_MODE_NUNITS (mode) == nunits
500         && GET_MODE_INNER (mode) == innermode)
501       break;
502
503   /* For integers, try mapping it to a same-sized scalar mode.  */
504   if (mode == VOIDmode
505       && GET_MODE_CLASS (innermode) == MODE_INT)
506     mode = mode_for_size (nunits * GET_MODE_BITSIZE (innermode),
507                           MODE_INT, 0);
508
509   if (mode == VOIDmode
510       || (GET_MODE_CLASS (mode) == MODE_INT
511           && !have_regs_of_mode[mode]))
512     return BLKmode;
513
514   return mode;
515 }
516
517 /* Return the alignment of MODE. This will be bounded by 1 and
518    BIGGEST_ALIGNMENT.  */
519
520 unsigned int
521 get_mode_alignment (enum machine_mode mode)
522 {
523   return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
524 }
525
526 \f
527 /* Subroutine of layout_decl: Force alignment required for the data type.
528    But if the decl itself wants greater alignment, don't override that.  */
529
530 static inline void
531 do_type_align (tree type, tree decl)
532 {
533   if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
534     {
535       DECL_ALIGN (decl) = TYPE_ALIGN (type);
536       if (TREE_CODE (decl) == FIELD_DECL)
537         DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
538     }
539 }
540
541 /* Set the size, mode and alignment of a ..._DECL node.
542    TYPE_DECL does need this for C++.
543    Note that LABEL_DECL and CONST_DECL nodes do not need this,
544    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
545    Don't call layout_decl for them.
546
547    KNOWN_ALIGN is the amount of alignment we can assume this
548    decl has with no special effort.  It is relevant only for FIELD_DECLs
549    and depends on the previous fields.
550    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
551    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
552    the record will be aligned to suit.  */
553
554 void
555 layout_decl (tree decl, unsigned int known_align)
556 {
557   tree type = TREE_TYPE (decl);
558   enum tree_code code = TREE_CODE (decl);
559   rtx rtl = NULL_RTX;
560   location_t loc = DECL_SOURCE_LOCATION (decl);
561
562   if (code == CONST_DECL)
563     return;
564
565   gcc_assert (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL
566               || code == TYPE_DECL ||code == FIELD_DECL);
567
568   rtl = DECL_RTL_IF_SET (decl);
569
570   if (type == error_mark_node)
571     type = void_type_node;
572
573   /* Usually the size and mode come from the data type without change,
574      however, the front-end may set the explicit width of the field, so its
575      size may not be the same as the size of its type.  This happens with
576      bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
577      also happens with other fields.  For example, the C++ front-end creates
578      zero-sized fields corresponding to empty base classes, and depends on
579      layout_type setting DECL_FIELD_BITPOS correctly for the field.  Set the
580      size in bytes from the size in bits.  If we have already set the mode,
581      don't set it again since we can be called twice for FIELD_DECLs.  */
582
583   DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
584   if (DECL_MODE (decl) == VOIDmode)
585     DECL_MODE (decl) = TYPE_MODE (type);
586
587   if (DECL_SIZE (decl) == 0)
588     {
589       DECL_SIZE (decl) = TYPE_SIZE (type);
590       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
591     }
592   else if (DECL_SIZE_UNIT (decl) == 0)
593     DECL_SIZE_UNIT (decl)
594       = fold_convert_loc (loc, sizetype,
595                           size_binop_loc (loc, CEIL_DIV_EXPR, DECL_SIZE (decl),
596                                           bitsize_unit_node));
597
598   if (code != FIELD_DECL)
599     /* For non-fields, update the alignment from the type.  */
600     do_type_align (type, decl);
601   else
602     /* For fields, it's a bit more complicated...  */
603     {
604       bool old_user_align = DECL_USER_ALIGN (decl);
605       bool zero_bitfield = false;
606       bool packed_p = DECL_PACKED (decl);
607       unsigned int mfa;
608
609       if (DECL_BIT_FIELD (decl))
610         {
611           DECL_BIT_FIELD_TYPE (decl) = type;
612
613           /* A zero-length bit-field affects the alignment of the next
614              field.  In essence such bit-fields are not influenced by
615              any packing due to #pragma pack or attribute packed.  */
616           if (integer_zerop (DECL_SIZE (decl))
617               && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
618             {
619               zero_bitfield = true;
620               packed_p = false;
621 #ifdef PCC_BITFIELD_TYPE_MATTERS
622               if (PCC_BITFIELD_TYPE_MATTERS)
623                 do_type_align (type, decl);
624               else
625 #endif
626                 {
627 #ifdef EMPTY_FIELD_BOUNDARY
628                   if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
629                     {
630                       DECL_ALIGN (decl) = EMPTY_FIELD_BOUNDARY;
631                       DECL_USER_ALIGN (decl) = 0;
632                     }
633 #endif
634                 }
635             }
636
637           /* See if we can use an ordinary integer mode for a bit-field.
638              Conditions are: a fixed size that is correct for another mode
639              and occupying a complete byte or bytes on proper boundary.  */
640           if (TYPE_SIZE (type) != 0
641               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
642               && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
643             {
644               enum machine_mode xmode
645                 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
646               unsigned int xalign = GET_MODE_ALIGNMENT (xmode);
647
648               if (xmode != BLKmode
649                   && !(xalign > BITS_PER_UNIT && DECL_PACKED (decl))
650                   && (known_align == 0 || known_align >= xalign))
651                 {
652                   DECL_ALIGN (decl) = MAX (xalign, DECL_ALIGN (decl));
653                   DECL_MODE (decl) = xmode;
654                   DECL_BIT_FIELD (decl) = 0;
655                 }
656             }
657
658           /* Turn off DECL_BIT_FIELD if we won't need it set.  */
659           if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
660               && known_align >= TYPE_ALIGN (type)
661               && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
662             DECL_BIT_FIELD (decl) = 0;
663         }
664       else if (packed_p && DECL_USER_ALIGN (decl))
665         /* Don't touch DECL_ALIGN.  For other packed fields, go ahead and
666            round up; we'll reduce it again below.  We want packing to
667            supersede USER_ALIGN inherited from the type, but defer to
668            alignment explicitly specified on the field decl.  */;
669       else
670         do_type_align (type, decl);
671
672       /* If the field is packed and not explicitly aligned, give it the
673          minimum alignment.  Note that do_type_align may set
674          DECL_USER_ALIGN, so we need to check old_user_align instead.  */
675       if (packed_p
676           && !old_user_align)
677         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
678
679       if (! packed_p && ! DECL_USER_ALIGN (decl))
680         {
681           /* Some targets (i.e. i386, VMS) limit struct field alignment
682              to a lower boundary than alignment of variables unless
683              it was overridden by attribute aligned.  */
684 #ifdef BIGGEST_FIELD_ALIGNMENT
685           DECL_ALIGN (decl)
686             = MIN (DECL_ALIGN (decl), (unsigned) BIGGEST_FIELD_ALIGNMENT);
687 #endif
688 #ifdef ADJUST_FIELD_ALIGN
689           DECL_ALIGN (decl) = ADJUST_FIELD_ALIGN (decl, DECL_ALIGN (decl));
690 #endif
691         }
692
693       if (zero_bitfield)
694         mfa = initial_max_fld_align * BITS_PER_UNIT;
695       else
696         mfa = maximum_field_alignment;
697       /* Should this be controlled by DECL_USER_ALIGN, too?  */
698       if (mfa != 0)
699         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), mfa);
700     }
701
702   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
703   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
704     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
705   if (DECL_SIZE_UNIT (decl) != 0
706       && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
707     DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
708
709   /* If requested, warn about definitions of large data objects.  */
710   if (warn_larger_than
711       && (code == VAR_DECL || code == PARM_DECL)
712       && ! DECL_EXTERNAL (decl))
713     {
714       tree size = DECL_SIZE_UNIT (decl);
715
716       if (size != 0 && TREE_CODE (size) == INTEGER_CST
717           && compare_tree_int (size, larger_than_size) > 0)
718         {
719           int size_as_int = TREE_INT_CST_LOW (size);
720
721           if (compare_tree_int (size, size_as_int) == 0)
722             warning (OPT_Wlarger_than_, "size of %q+D is %d bytes", decl, size_as_int);
723           else
724             warning (OPT_Wlarger_than_, "size of %q+D is larger than %wd bytes",
725                      decl, larger_than_size);
726         }
727     }
728
729   /* If the RTL was already set, update its mode and mem attributes.  */
730   if (rtl)
731     {
732       PUT_MODE (rtl, DECL_MODE (decl));
733       SET_DECL_RTL (decl, 0);
734       set_mem_attributes (rtl, decl, 1);
735       SET_DECL_RTL (decl, rtl);
736     }
737 }
738
739 /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of
740    a previous call to layout_decl and calls it again.  */
741
742 void
743 relayout_decl (tree decl)
744 {
745   DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
746   DECL_MODE (decl) = VOIDmode;
747   if (!DECL_USER_ALIGN (decl))
748     DECL_ALIGN (decl) = 0;
749   SET_DECL_RTL (decl, 0);
750
751   layout_decl (decl, 0);
752 }
753 \f
754 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
755    QUAL_UNION_TYPE.  Return a pointer to a struct record_layout_info which
756    is to be passed to all other layout functions for this record.  It is the
757    responsibility of the caller to call `free' for the storage returned.
758    Note that garbage collection is not permitted until we finish laying
759    out the record.  */
760
761 record_layout_info
762 start_record_layout (tree t)
763 {
764   record_layout_info rli = XNEW (struct record_layout_info_s);
765
766   rli->t = t;
767
768   /* If the type has a minimum specified alignment (via an attribute
769      declaration, for example) use it -- otherwise, start with a
770      one-byte alignment.  */
771   rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
772   rli->unpacked_align = rli->record_align;
773   rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
774
775 #ifdef STRUCTURE_SIZE_BOUNDARY
776   /* Packed structures don't need to have minimum size.  */
777   if (! TYPE_PACKED (t))
778     {
779       unsigned tmp;
780
781       /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY.  */
782       tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
783       if (maximum_field_alignment != 0)
784         tmp = MIN (tmp, maximum_field_alignment);
785       rli->record_align = MAX (rli->record_align, tmp);
786     }
787 #endif
788
789   rli->offset = size_zero_node;
790   rli->bitpos = bitsize_zero_node;
791   rli->prev_field = 0;
792   rli->pending_statics = NULL;
793   rli->packed_maybe_necessary = 0;
794   rli->remaining_in_alignment = 0;
795
796   return rli;
797 }
798
799 /* These four routines perform computations that convert between
800    the offset/bitpos forms and byte and bit offsets.  */
801
802 tree
803 bit_from_pos (tree offset, tree bitpos)
804 {
805   return size_binop (PLUS_EXPR, bitpos,
806                      size_binop (MULT_EXPR,
807                                  fold_convert (bitsizetype, offset),
808                                  bitsize_unit_node));
809 }
810
811 tree
812 byte_from_pos (tree offset, tree bitpos)
813 {
814   return size_binop (PLUS_EXPR, offset,
815                      fold_convert (sizetype,
816                                    size_binop (TRUNC_DIV_EXPR, bitpos,
817                                                bitsize_unit_node)));
818 }
819
820 void
821 pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
822               tree pos)
823 {
824   *poffset = size_binop (MULT_EXPR,
825                          fold_convert (sizetype,
826                                        size_binop (FLOOR_DIV_EXPR, pos,
827                                                    bitsize_int (off_align))),
828                          size_int (off_align / BITS_PER_UNIT));
829   *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
830 }
831
832 /* Given a pointer to bit and byte offsets and an offset alignment,
833    normalize the offsets so they are within the alignment.  */
834
835 void
836 normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
837 {
838   /* If the bit position is now larger than it should be, adjust it
839      downwards.  */
840   if (compare_tree_int (*pbitpos, off_align) >= 0)
841     {
842       tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
843                                       bitsize_int (off_align));
844
845       *poffset
846         = size_binop (PLUS_EXPR, *poffset,
847                       size_binop (MULT_EXPR,
848                                   fold_convert (sizetype, extra_aligns),
849                                   size_int (off_align / BITS_PER_UNIT)));
850
851       *pbitpos
852         = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
853     }
854 }
855
856 /* Print debugging information about the information in RLI.  */
857
858 DEBUG_FUNCTION void
859 debug_rli (record_layout_info rli)
860 {
861   print_node_brief (stderr, "type", rli->t, 0);
862   print_node_brief (stderr, "\noffset", rli->offset, 0);
863   print_node_brief (stderr, " bitpos", rli->bitpos, 0);
864
865   fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
866            rli->record_align, rli->unpacked_align,
867            rli->offset_align);
868
869   /* The ms_struct code is the only that uses this.  */
870   if (targetm.ms_bitfield_layout_p (rli->t))
871     fprintf (stderr, "remaining in alignment = %u\n", rli->remaining_in_alignment);
872
873   if (rli->packed_maybe_necessary)
874     fprintf (stderr, "packed may be necessary\n");
875
876   if (!VEC_empty (tree, rli->pending_statics))
877     {
878       fprintf (stderr, "pending statics:\n");
879       debug_vec_tree (rli->pending_statics);
880     }
881 }
882
883 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
884    BITPOS if necessary to keep BITPOS below OFFSET_ALIGN.  */
885
886 void
887 normalize_rli (record_layout_info rli)
888 {
889   normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
890 }
891
892 /* Returns the size in bytes allocated so far.  */
893
894 tree
895 rli_size_unit_so_far (record_layout_info rli)
896 {
897   return byte_from_pos (rli->offset, rli->bitpos);
898 }
899
900 /* Returns the size in bits allocated so far.  */
901
902 tree
903 rli_size_so_far (record_layout_info rli)
904 {
905   return bit_from_pos (rli->offset, rli->bitpos);
906 }
907
908 /* FIELD is about to be added to RLI->T.  The alignment (in bits) of
909    the next available location within the record is given by KNOWN_ALIGN.
910    Update the variable alignment fields in RLI, and return the alignment
911    to give the FIELD.  */
912
913 unsigned int
914 update_alignment_for_field (record_layout_info rli, tree field,
915                             unsigned int known_align)
916 {
917   /* The alignment required for FIELD.  */
918   unsigned int desired_align;
919   /* The type of this field.  */
920   tree type = TREE_TYPE (field);
921   /* True if the field was explicitly aligned by the user.  */
922   bool user_align;
923   bool is_bitfield;
924
925   /* Do not attempt to align an ERROR_MARK node */
926   if (TREE_CODE (type) == ERROR_MARK)
927     return 0;
928
929   /* Lay out the field so we know what alignment it needs.  */
930   layout_decl (field, known_align);
931   desired_align = DECL_ALIGN (field);
932   user_align = DECL_USER_ALIGN (field);
933
934   is_bitfield = (type != error_mark_node
935                  && DECL_BIT_FIELD_TYPE (field)
936                  && ! integer_zerop (TYPE_SIZE (type)));
937
938   /* Record must have at least as much alignment as any field.
939      Otherwise, the alignment of the field within the record is
940      meaningless.  */
941   if (targetm.ms_bitfield_layout_p (rli->t))
942     {
943       /* Here, the alignment of the underlying type of a bitfield can
944          affect the alignment of a record; even a zero-sized field
945          can do this.  The alignment should be to the alignment of
946          the type, except that for zero-size bitfields this only
947          applies if there was an immediately prior, nonzero-size
948          bitfield.  (That's the way it is, experimentally.) */
949       if ((!is_bitfield && !DECL_PACKED (field))
950           || (!integer_zerop (DECL_SIZE (field))
951               ? !DECL_PACKED (field)
952               : (rli->prev_field
953                  && DECL_BIT_FIELD_TYPE (rli->prev_field)
954                  && ! integer_zerop (DECL_SIZE (rli->prev_field)))))
955         {
956           unsigned int type_align = TYPE_ALIGN (type);
957           type_align = MAX (type_align, desired_align);
958           if (maximum_field_alignment != 0)
959             type_align = MIN (type_align, maximum_field_alignment);
960           rli->record_align = MAX (rli->record_align, type_align);
961           rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
962         }
963     }
964 #ifdef PCC_BITFIELD_TYPE_MATTERS
965   else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
966     {
967       /* Named bit-fields cause the entire structure to have the
968          alignment implied by their type.  Some targets also apply the same
969          rules to unnamed bitfields.  */
970       if (DECL_NAME (field) != 0
971           || targetm.align_anon_bitfield ())
972         {
973           unsigned int type_align = TYPE_ALIGN (type);
974
975 #ifdef ADJUST_FIELD_ALIGN
976           if (! TYPE_USER_ALIGN (type))
977             type_align = ADJUST_FIELD_ALIGN (field, type_align);
978 #endif
979
980           /* Targets might chose to handle unnamed and hence possibly
981              zero-width bitfield.  Those are not influenced by #pragmas
982              or packed attributes.  */
983           if (integer_zerop (DECL_SIZE (field)))
984             {
985               if (initial_max_fld_align)
986                 type_align = MIN (type_align,
987                                   initial_max_fld_align * BITS_PER_UNIT);
988             }
989           else if (maximum_field_alignment != 0)
990             type_align = MIN (type_align, maximum_field_alignment);
991           else if (DECL_PACKED (field))
992             type_align = MIN (type_align, BITS_PER_UNIT);
993
994           /* The alignment of the record is increased to the maximum
995              of the current alignment, the alignment indicated on the
996              field (i.e., the alignment specified by an __aligned__
997              attribute), and the alignment indicated by the type of
998              the field.  */
999           rli->record_align = MAX (rli->record_align, desired_align);
1000           rli->record_align = MAX (rli->record_align, type_align);
1001
1002           if (warn_packed)
1003             rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1004           user_align |= TYPE_USER_ALIGN (type);
1005         }
1006     }
1007 #endif
1008   else
1009     {
1010       rli->record_align = MAX (rli->record_align, desired_align);
1011       rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1012     }
1013
1014   TYPE_USER_ALIGN (rli->t) |= user_align;
1015
1016   return desired_align;
1017 }
1018
1019 /* Called from place_field to handle unions.  */
1020
1021 static void
1022 place_union_field (record_layout_info rli, tree field)
1023 {
1024   update_alignment_for_field (rli, field, /*known_align=*/0);
1025
1026   DECL_FIELD_OFFSET (field) = size_zero_node;
1027   DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
1028   SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
1029
1030   /* If this is an ERROR_MARK return *after* having set the
1031      field at the start of the union. This helps when parsing
1032      invalid fields. */
1033   if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1034     return;
1035
1036   /* We assume the union's size will be a multiple of a byte so we don't
1037      bother with BITPOS.  */
1038   if (TREE_CODE (rli->t) == UNION_TYPE)
1039     rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1040   else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
1041     rli->offset = fold_build3_loc (input_location, COND_EXPR, sizetype,
1042                                DECL_QUALIFIER (field),
1043                                DECL_SIZE_UNIT (field), rli->offset);
1044 }
1045
1046 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
1047 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
1048    at BYTE_OFFSET / BIT_OFFSET.  Return nonzero if the field would span more
1049    units of alignment than the underlying TYPE.  */
1050 static int
1051 excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
1052                   HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
1053 {
1054   /* Note that the calculation of OFFSET might overflow; we calculate it so
1055      that we still get the right result as long as ALIGN is a power of two.  */
1056   unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
1057
1058   offset = offset % align;
1059   return ((offset + size + align - 1) / align
1060           > ((unsigned HOST_WIDE_INT) tree_low_cst (TYPE_SIZE (type), 1)
1061              / align));
1062 }
1063 #endif
1064
1065 /* RLI contains information about the layout of a RECORD_TYPE.  FIELD
1066    is a FIELD_DECL to be added after those fields already present in
1067    T.  (FIELD is not actually added to the TYPE_FIELDS list here;
1068    callers that desire that behavior must manually perform that step.)  */
1069
1070 void
1071 place_field (record_layout_info rli, tree field)
1072 {
1073   /* The alignment required for FIELD.  */
1074   unsigned int desired_align;
1075   /* The alignment FIELD would have if we just dropped it into the
1076      record as it presently stands.  */
1077   unsigned int known_align;
1078   unsigned int actual_align;
1079   /* The type of this field.  */
1080   tree type = TREE_TYPE (field);
1081
1082   gcc_assert (TREE_CODE (field) != ERROR_MARK);
1083
1084   /* If FIELD is static, then treat it like a separate variable, not
1085      really like a structure field.  If it is a FUNCTION_DECL, it's a
1086      method.  In both cases, all we do is lay out the decl, and we do
1087      it *after* the record is laid out.  */
1088   if (TREE_CODE (field) == VAR_DECL)
1089     {
1090       VEC_safe_push (tree, gc, rli->pending_statics, field);
1091       return;
1092     }
1093
1094   /* Enumerators and enum types which are local to this class need not
1095      be laid out.  Likewise for initialized constant fields.  */
1096   else if (TREE_CODE (field) != FIELD_DECL)
1097     return;
1098
1099   /* Unions are laid out very differently than records, so split
1100      that code off to another function.  */
1101   else if (TREE_CODE (rli->t) != RECORD_TYPE)
1102     {
1103       place_union_field (rli, field);
1104       return;
1105     }
1106
1107   else if (TREE_CODE (type) == ERROR_MARK)
1108     {
1109       /* Place this field at the current allocation position, so we
1110          maintain monotonicity.  */
1111       DECL_FIELD_OFFSET (field) = rli->offset;
1112       DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1113       SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1114       return;
1115     }
1116
1117   /* Work out the known alignment so far.  Note that A & (-A) is the
1118      value of the least-significant bit in A that is one.  */
1119   if (! integer_zerop (rli->bitpos))
1120     known_align = (tree_low_cst (rli->bitpos, 1)
1121                    & - tree_low_cst (rli->bitpos, 1));
1122   else if (integer_zerop (rli->offset))
1123     known_align = 0;
1124   else if (host_integerp (rli->offset, 1))
1125     known_align = (BITS_PER_UNIT
1126                    * (tree_low_cst (rli->offset, 1)
1127                       & - tree_low_cst (rli->offset, 1)));
1128   else
1129     known_align = rli->offset_align;
1130
1131   desired_align = update_alignment_for_field (rli, field, known_align);
1132   if (known_align == 0)
1133     known_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1134
1135   if (warn_packed && DECL_PACKED (field))
1136     {
1137       if (known_align >= TYPE_ALIGN (type))
1138         {
1139           if (TYPE_ALIGN (type) > desired_align)
1140             {
1141               if (STRICT_ALIGNMENT)
1142                 warning (OPT_Wattributes, "packed attribute causes "
1143                          "inefficient alignment for %q+D", field);
1144               /* Don't warn if DECL_PACKED was set by the type.  */
1145               else if (!TYPE_PACKED (rli->t))
1146                 warning (OPT_Wattributes, "packed attribute is "
1147                          "unnecessary for %q+D", field);
1148             }
1149         }
1150       else
1151         rli->packed_maybe_necessary = 1;
1152     }
1153
1154   /* Does this field automatically have alignment it needs by virtue
1155      of the fields that precede it and the record's own alignment?
1156      We already align ms_struct fields, so don't re-align them.  */
1157   if (known_align < desired_align
1158       && !targetm.ms_bitfield_layout_p (rli->t))
1159     {
1160       /* No, we need to skip space before this field.
1161          Bump the cumulative size to multiple of field alignment.  */
1162
1163       if (DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION)
1164         warning (OPT_Wpadded, "padding struct to align %q+D", field);
1165
1166       /* If the alignment is still within offset_align, just align
1167          the bit position.  */
1168       if (desired_align < rli->offset_align)
1169         rli->bitpos = round_up (rli->bitpos, desired_align);
1170       else
1171         {
1172           /* First adjust OFFSET by the partial bits, then align.  */
1173           rli->offset
1174             = size_binop (PLUS_EXPR, rli->offset,
1175                           fold_convert (sizetype,
1176                                         size_binop (CEIL_DIV_EXPR, rli->bitpos,
1177                                                     bitsize_unit_node)));
1178           rli->bitpos = bitsize_zero_node;
1179
1180           rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
1181         }
1182
1183       if (! TREE_CONSTANT (rli->offset))
1184         rli->offset_align = desired_align;
1185
1186     }
1187
1188   /* Handle compatibility with PCC.  Note that if the record has any
1189      variable-sized fields, we need not worry about compatibility.  */
1190 #ifdef PCC_BITFIELD_TYPE_MATTERS
1191   if (PCC_BITFIELD_TYPE_MATTERS
1192       && ! targetm.ms_bitfield_layout_p (rli->t)
1193       && TREE_CODE (field) == FIELD_DECL
1194       && type != error_mark_node
1195       && DECL_BIT_FIELD (field)
1196       && (! DECL_PACKED (field)
1197           /* Enter for these packed fields only to issue a warning.  */
1198           || TYPE_ALIGN (type) <= BITS_PER_UNIT)
1199       && maximum_field_alignment == 0
1200       && ! integer_zerop (DECL_SIZE (field))
1201       && host_integerp (DECL_SIZE (field), 1)
1202       && host_integerp (rli->offset, 1)
1203       && host_integerp (TYPE_SIZE (type), 1))
1204     {
1205       unsigned int type_align = TYPE_ALIGN (type);
1206       tree dsize = DECL_SIZE (field);
1207       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
1208       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
1209       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
1210
1211 #ifdef ADJUST_FIELD_ALIGN
1212       if (! TYPE_USER_ALIGN (type))
1213         type_align = ADJUST_FIELD_ALIGN (field, type_align);
1214 #endif
1215
1216       /* A bit field may not span more units of alignment of its type
1217          than its type itself.  Advance to next boundary if necessary.  */
1218       if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1219         {
1220           if (DECL_PACKED (field))
1221             {
1222               if (warn_packed_bitfield_compat == 1)
1223                 inform
1224                   (input_location,
1225                    "Offset of packed bit-field %qD has changed in GCC 4.4",
1226                    field);
1227             }
1228           else
1229             rli->bitpos = round_up_loc (input_location, rli->bitpos, type_align);
1230         }
1231
1232       if (! DECL_PACKED (field))
1233         TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1234     }
1235 #endif
1236
1237 #ifdef BITFIELD_NBYTES_LIMITED
1238   if (BITFIELD_NBYTES_LIMITED
1239       && ! targetm.ms_bitfield_layout_p (rli->t)
1240       && TREE_CODE (field) == FIELD_DECL
1241       && type != error_mark_node
1242       && DECL_BIT_FIELD_TYPE (field)
1243       && ! DECL_PACKED (field)
1244       && ! integer_zerop (DECL_SIZE (field))
1245       && host_integerp (DECL_SIZE (field), 1)
1246       && host_integerp (rli->offset, 1)
1247       && host_integerp (TYPE_SIZE (type), 1))
1248     {
1249       unsigned int type_align = TYPE_ALIGN (type);
1250       tree dsize = DECL_SIZE (field);
1251       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
1252       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
1253       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
1254
1255 #ifdef ADJUST_FIELD_ALIGN
1256       if (! TYPE_USER_ALIGN (type))
1257         type_align = ADJUST_FIELD_ALIGN (field, type_align);
1258 #endif
1259
1260       if (maximum_field_alignment != 0)
1261         type_align = MIN (type_align, maximum_field_alignment);
1262       /* ??? This test is opposite the test in the containing if
1263          statement, so this code is unreachable currently.  */
1264       else if (DECL_PACKED (field))
1265         type_align = MIN (type_align, BITS_PER_UNIT);
1266
1267       /* A bit field may not span the unit of alignment of its type.
1268          Advance to next boundary if necessary.  */
1269       if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1270         rli->bitpos = round_up (rli->bitpos, type_align);
1271
1272       TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1273     }
1274 #endif
1275
1276   /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
1277      A subtlety:
1278         When a bit field is inserted into a packed record, the whole
1279         size of the underlying type is used by one or more same-size
1280         adjacent bitfields.  (That is, if its long:3, 32 bits is
1281         used in the record, and any additional adjacent long bitfields are
1282         packed into the same chunk of 32 bits. However, if the size
1283         changes, a new field of that size is allocated.)  In an unpacked
1284         record, this is the same as using alignment, but not equivalent
1285         when packing.
1286
1287      Note: for compatibility, we use the type size, not the type alignment
1288      to determine alignment, since that matches the documentation */
1289
1290   if (targetm.ms_bitfield_layout_p (rli->t))
1291     {
1292       tree prev_saved = rli->prev_field;
1293       tree prev_type = prev_saved ? DECL_BIT_FIELD_TYPE (prev_saved) : NULL;
1294
1295       /* This is a bitfield if it exists.  */
1296       if (rli->prev_field)
1297         {
1298           /* If both are bitfields, nonzero, and the same size, this is
1299              the middle of a run.  Zero declared size fields are special
1300              and handled as "end of run". (Note: it's nonzero declared
1301              size, but equal type sizes!) (Since we know that both
1302              the current and previous fields are bitfields by the
1303              time we check it, DECL_SIZE must be present for both.) */
1304           if (DECL_BIT_FIELD_TYPE (field)
1305               && !integer_zerop (DECL_SIZE (field))
1306               && !integer_zerop (DECL_SIZE (rli->prev_field))
1307               && host_integerp (DECL_SIZE (rli->prev_field), 0)
1308               && host_integerp (TYPE_SIZE (type), 0)
1309               && simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type)))
1310             {
1311               /* We're in the middle of a run of equal type size fields; make
1312                  sure we realign if we run out of bits.  (Not decl size,
1313                  type size!) */
1314               HOST_WIDE_INT bitsize = tree_low_cst (DECL_SIZE (field), 1);
1315
1316               if (rli->remaining_in_alignment < bitsize)
1317                 {
1318                   HOST_WIDE_INT typesize = tree_low_cst (TYPE_SIZE (type), 1);
1319
1320                   /* out of bits; bump up to next 'word'.  */
1321                   rli->bitpos
1322                     = size_binop (PLUS_EXPR, rli->bitpos,
1323                                   bitsize_int (rli->remaining_in_alignment));
1324                   rli->prev_field = field;
1325                   if (typesize < bitsize)
1326                     rli->remaining_in_alignment = 0;
1327                   else
1328                     rli->remaining_in_alignment = typesize - bitsize;
1329                 }
1330               else
1331                 rli->remaining_in_alignment -= bitsize;
1332             }
1333           else
1334             {
1335               /* End of a run: if leaving a run of bitfields of the same type
1336                  size, we have to "use up" the rest of the bits of the type
1337                  size.
1338
1339                  Compute the new position as the sum of the size for the prior
1340                  type and where we first started working on that type.
1341                  Note: since the beginning of the field was aligned then
1342                  of course the end will be too.  No round needed.  */
1343
1344               if (!integer_zerop (DECL_SIZE (rli->prev_field)))
1345                 {
1346                   rli->bitpos
1347                     = size_binop (PLUS_EXPR, rli->bitpos,
1348                                   bitsize_int (rli->remaining_in_alignment));
1349                 }
1350               else
1351                 /* We "use up" size zero fields; the code below should behave
1352                    as if the prior field was not a bitfield.  */
1353                 prev_saved = NULL;
1354
1355               /* Cause a new bitfield to be captured, either this time (if
1356                  currently a bitfield) or next time we see one.  */
1357               if (!DECL_BIT_FIELD_TYPE(field)
1358                   || integer_zerop (DECL_SIZE (field)))
1359                 rli->prev_field = NULL;
1360             }
1361
1362           normalize_rli (rli);
1363         }
1364
1365       /* If we're starting a new run of same size type bitfields
1366          (or a run of non-bitfields), set up the "first of the run"
1367          fields.
1368
1369          That is, if the current field is not a bitfield, or if there
1370          was a prior bitfield the type sizes differ, or if there wasn't
1371          a prior bitfield the size of the current field is nonzero.
1372
1373          Note: we must be sure to test ONLY the type size if there was
1374          a prior bitfield and ONLY for the current field being zero if
1375          there wasn't.  */
1376
1377       if (!DECL_BIT_FIELD_TYPE (field)
1378           || (prev_saved != NULL
1379               ? !simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type))
1380               : !integer_zerop (DECL_SIZE (field)) ))
1381         {
1382           /* Never smaller than a byte for compatibility.  */
1383           unsigned int type_align = BITS_PER_UNIT;
1384
1385           /* (When not a bitfield), we could be seeing a flex array (with
1386              no DECL_SIZE).  Since we won't be using remaining_in_alignment
1387              until we see a bitfield (and come by here again) we just skip
1388              calculating it.  */
1389           if (DECL_SIZE (field) != NULL
1390               && host_integerp (TYPE_SIZE (TREE_TYPE (field)), 1)
1391               && host_integerp (DECL_SIZE (field), 1))
1392             {
1393               unsigned HOST_WIDE_INT bitsize
1394                 = tree_low_cst (DECL_SIZE (field), 1);
1395               unsigned HOST_WIDE_INT typesize
1396                 = tree_low_cst (TYPE_SIZE (TREE_TYPE (field)), 1);
1397
1398               if (typesize < bitsize)
1399                 rli->remaining_in_alignment = 0;
1400               else
1401                 rli->remaining_in_alignment = typesize - bitsize;
1402             }
1403
1404           /* Now align (conventionally) for the new type.  */
1405           type_align = TYPE_ALIGN (TREE_TYPE (field));
1406
1407           if (maximum_field_alignment != 0)
1408             type_align = MIN (type_align, maximum_field_alignment);
1409
1410           rli->bitpos = round_up_loc (input_location, rli->bitpos, type_align);
1411
1412           /* If we really aligned, don't allow subsequent bitfields
1413              to undo that.  */
1414           rli->prev_field = NULL;
1415         }
1416     }
1417
1418   /* Offset so far becomes the position of this field after normalizing.  */
1419   normalize_rli (rli);
1420   DECL_FIELD_OFFSET (field) = rli->offset;
1421   DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1422   SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1423
1424   /* If this field ended up more aligned than we thought it would be (we
1425      approximate this by seeing if its position changed), lay out the field
1426      again; perhaps we can use an integral mode for it now.  */
1427   if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1428     actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1429                     & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1430   else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1431     actual_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1432   else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1433     actual_align = (BITS_PER_UNIT
1434                    * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1435                       & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1436   else
1437     actual_align = DECL_OFFSET_ALIGN (field);
1438   /* ACTUAL_ALIGN is still the actual alignment *within the record* .
1439      store / extract bit field operations will check the alignment of the
1440      record against the mode of bit fields.  */
1441
1442   if (known_align != actual_align)
1443     layout_decl (field, actual_align);
1444
1445   if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE (field))
1446     rli->prev_field = field;
1447
1448   /* Now add size of this field to the size of the record.  If the size is
1449      not constant, treat the field as being a multiple of bytes and just
1450      adjust the offset, resetting the bit position.  Otherwise, apportion the
1451      size amongst the bit position and offset.  First handle the case of an
1452      unspecified size, which can happen when we have an invalid nested struct
1453      definition, such as struct j { struct j { int i; } }.  The error message
1454      is printed in finish_struct.  */
1455   if (DECL_SIZE (field) == 0)
1456     /* Do nothing.  */;
1457   else if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
1458            || TREE_OVERFLOW (DECL_SIZE (field)))
1459     {
1460       rli->offset
1461         = size_binop (PLUS_EXPR, rli->offset,
1462                       fold_convert (sizetype,
1463                                     size_binop (CEIL_DIV_EXPR, rli->bitpos,
1464                                                 bitsize_unit_node)));
1465       rli->offset
1466         = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1467       rli->bitpos = bitsize_zero_node;
1468       rli->offset_align = MIN (rli->offset_align, desired_align);
1469     }
1470   else if (targetm.ms_bitfield_layout_p (rli->t))
1471     {
1472       rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1473
1474       /* If we ended a bitfield before the full length of the type then
1475          pad the struct out to the full length of the last type.  */
1476       if ((DECL_CHAIN (field) == NULL
1477            || TREE_CODE (DECL_CHAIN (field)) != FIELD_DECL)
1478           && DECL_BIT_FIELD_TYPE (field)
1479           && !integer_zerop (DECL_SIZE (field)))
1480         rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos,
1481                                   bitsize_int (rli->remaining_in_alignment));
1482
1483       normalize_rli (rli);
1484     }
1485   else
1486     {
1487       rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1488       normalize_rli (rli);
1489     }
1490 }
1491
1492 /* Assuming that all the fields have been laid out, this function uses
1493    RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1494    indicated by RLI.  */
1495
1496 static void
1497 finalize_record_size (record_layout_info rli)
1498 {
1499   tree unpadded_size, unpadded_size_unit;
1500
1501   /* Now we want just byte and bit offsets, so set the offset alignment
1502      to be a byte and then normalize.  */
1503   rli->offset_align = BITS_PER_UNIT;
1504   normalize_rli (rli);
1505
1506   /* Determine the desired alignment.  */
1507 #ifdef ROUND_TYPE_ALIGN
1508   TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1509                                           rli->record_align);
1510 #else
1511   TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1512 #endif
1513
1514   /* Compute the size so far.  Be sure to allow for extra bits in the
1515      size in bytes.  We have guaranteed above that it will be no more
1516      than a single byte.  */
1517   unpadded_size = rli_size_so_far (rli);
1518   unpadded_size_unit = rli_size_unit_so_far (rli);
1519   if (! integer_zerop (rli->bitpos))
1520     unpadded_size_unit
1521       = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1522
1523   /* Round the size up to be a multiple of the required alignment.  */
1524   TYPE_SIZE (rli->t) = round_up_loc (input_location, unpadded_size,
1525                                  TYPE_ALIGN (rli->t));
1526   TYPE_SIZE_UNIT (rli->t)
1527     = round_up_loc (input_location, unpadded_size_unit, TYPE_ALIGN_UNIT (rli->t));
1528
1529   if (TREE_CONSTANT (unpadded_size)
1530       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
1531       && input_location != BUILTINS_LOCATION)
1532     warning (OPT_Wpadded, "padding struct size to alignment boundary");
1533
1534   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1535       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1536       && TREE_CONSTANT (unpadded_size))
1537     {
1538       tree unpacked_size;
1539
1540 #ifdef ROUND_TYPE_ALIGN
1541       rli->unpacked_align
1542         = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1543 #else
1544       rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1545 #endif
1546
1547       unpacked_size = round_up_loc (input_location, TYPE_SIZE (rli->t), rli->unpacked_align);
1548       if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1549         {
1550           if (TYPE_NAME (rli->t))
1551             {
1552               tree name;
1553
1554               if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1555                 name = TYPE_NAME (rli->t);
1556               else
1557                 name = DECL_NAME (TYPE_NAME (rli->t));
1558
1559               if (STRICT_ALIGNMENT)
1560                 warning (OPT_Wpacked, "packed attribute causes inefficient "
1561                          "alignment for %qE", name);
1562               else
1563                 warning (OPT_Wpacked,
1564                          "packed attribute is unnecessary for %qE", name);
1565             }
1566           else
1567             {
1568               if (STRICT_ALIGNMENT)
1569                 warning (OPT_Wpacked,
1570                          "packed attribute causes inefficient alignment");
1571               else
1572                 warning (OPT_Wpacked, "packed attribute is unnecessary");
1573             }
1574         }
1575     }
1576 }
1577
1578 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE).  */
1579
1580 void
1581 compute_record_mode (tree type)
1582 {
1583   tree field;
1584   enum machine_mode mode = VOIDmode;
1585
1586   /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1587      However, if possible, we use a mode that fits in a register
1588      instead, in order to allow for better optimization down the
1589      line.  */
1590   SET_TYPE_MODE (type, BLKmode);
1591
1592   if (! host_integerp (TYPE_SIZE (type), 1))
1593     return;
1594
1595   /* A record which has any BLKmode members must itself be
1596      BLKmode; it can't go in a register.  Unless the member is
1597      BLKmode only because it isn't aligned.  */
1598   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1599     {
1600       if (TREE_CODE (field) != FIELD_DECL)
1601         continue;
1602
1603       if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1604           || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1605               && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
1606               && !(TYPE_SIZE (TREE_TYPE (field)) != 0
1607                    && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
1608           || ! host_integerp (bit_position (field), 1)
1609           || DECL_SIZE (field) == 0
1610           || ! host_integerp (DECL_SIZE (field), 1))
1611         return;
1612
1613       /* If this field is the whole struct, remember its mode so
1614          that, say, we can put a double in a class into a DF
1615          register instead of forcing it to live in the stack.  */
1616       if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1617         mode = DECL_MODE (field);
1618
1619 #ifdef MEMBER_TYPE_FORCES_BLK
1620       /* With some targets, eg. c4x, it is sub-optimal
1621          to access an aligned BLKmode structure as a scalar.  */
1622
1623       if (MEMBER_TYPE_FORCES_BLK (field, mode))
1624         return;
1625 #endif /* MEMBER_TYPE_FORCES_BLK  */
1626     }
1627
1628   /* If we only have one real field; use its mode if that mode's size
1629      matches the type's size.  This only applies to RECORD_TYPE.  This
1630      does not apply to unions.  */
1631   if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode
1632       && host_integerp (TYPE_SIZE (type), 1)
1633       && GET_MODE_BITSIZE (mode) == TREE_INT_CST_LOW (TYPE_SIZE (type)))
1634     SET_TYPE_MODE (type, mode);
1635   else
1636     SET_TYPE_MODE (type, mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1));
1637
1638   /* If structure's known alignment is less than what the scalar
1639      mode would need, and it matters, then stick with BLKmode.  */
1640   if (TYPE_MODE (type) != BLKmode
1641       && STRICT_ALIGNMENT
1642       && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1643             || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1644     {
1645       /* If this is the only reason this type is BLKmode, then
1646          don't force containing types to be BLKmode.  */
1647       TYPE_NO_FORCE_BLK (type) = 1;
1648       SET_TYPE_MODE (type, BLKmode);
1649     }
1650 }
1651
1652 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1653    out.  */
1654
1655 static void
1656 finalize_type_size (tree type)
1657 {
1658   /* Normally, use the alignment corresponding to the mode chosen.
1659      However, where strict alignment is not required, avoid
1660      over-aligning structures, since most compilers do not do this
1661      alignment.  */
1662
1663   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1664       && (STRICT_ALIGNMENT
1665           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1666               && TREE_CODE (type) != QUAL_UNION_TYPE
1667               && TREE_CODE (type) != ARRAY_TYPE)))
1668     {
1669       unsigned mode_align = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1670
1671       /* Don't override a larger alignment requirement coming from a user
1672          alignment of one of the fields.  */
1673       if (mode_align >= TYPE_ALIGN (type))
1674         {
1675           TYPE_ALIGN (type) = mode_align;
1676           TYPE_USER_ALIGN (type) = 0;
1677         }
1678     }
1679
1680   /* Do machine-dependent extra alignment.  */
1681 #ifdef ROUND_TYPE_ALIGN
1682   TYPE_ALIGN (type)
1683     = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1684 #endif
1685
1686   /* If we failed to find a simple way to calculate the unit size
1687      of the type, find it by division.  */
1688   if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1689     /* TYPE_SIZE (type) is computed in bitsizetype.  After the division, the
1690        result will fit in sizetype.  We will get more efficient code using
1691        sizetype, so we force a conversion.  */
1692     TYPE_SIZE_UNIT (type)
1693       = fold_convert (sizetype,
1694                       size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1695                                   bitsize_unit_node));
1696
1697   if (TYPE_SIZE (type) != 0)
1698     {
1699       TYPE_SIZE (type) = round_up_loc (input_location,
1700                                    TYPE_SIZE (type), TYPE_ALIGN (type));
1701       TYPE_SIZE_UNIT (type) = round_up_loc (input_location, TYPE_SIZE_UNIT (type),
1702                                         TYPE_ALIGN_UNIT (type));
1703     }
1704
1705   /* Evaluate nonconstant sizes only once, either now or as soon as safe.  */
1706   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1707     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1708   if (TYPE_SIZE_UNIT (type) != 0
1709       && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1710     TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1711
1712   /* Also layout any other variants of the type.  */
1713   if (TYPE_NEXT_VARIANT (type)
1714       || type != TYPE_MAIN_VARIANT (type))
1715     {
1716       tree variant;
1717       /* Record layout info of this variant.  */
1718       tree size = TYPE_SIZE (type);
1719       tree size_unit = TYPE_SIZE_UNIT (type);
1720       unsigned int align = TYPE_ALIGN (type);
1721       unsigned int user_align = TYPE_USER_ALIGN (type);
1722       enum machine_mode mode = TYPE_MODE (type);
1723
1724       /* Copy it into all variants.  */
1725       for (variant = TYPE_MAIN_VARIANT (type);
1726            variant != 0;
1727            variant = TYPE_NEXT_VARIANT (variant))
1728         {
1729           TYPE_SIZE (variant) = size;
1730           TYPE_SIZE_UNIT (variant) = size_unit;
1731           TYPE_ALIGN (variant) = align;
1732           TYPE_USER_ALIGN (variant) = user_align;
1733           SET_TYPE_MODE (variant, mode);
1734         }
1735     }
1736 }
1737
1738 /* Do all of the work required to layout the type indicated by RLI,
1739    once the fields have been laid out.  This function will call `free'
1740    for RLI, unless FREE_P is false.  Passing a value other than false
1741    for FREE_P is bad practice; this option only exists to support the
1742    G++ 3.2 ABI.  */
1743
1744 void
1745 finish_record_layout (record_layout_info rli, int free_p)
1746 {
1747   tree variant;
1748
1749   /* Compute the final size.  */
1750   finalize_record_size (rli);
1751
1752   /* Compute the TYPE_MODE for the record.  */
1753   compute_record_mode (rli->t);
1754
1755   /* Perform any last tweaks to the TYPE_SIZE, etc.  */
1756   finalize_type_size (rli->t);
1757
1758   /* Propagate TYPE_PACKED to variants.  With C++ templates,
1759      handle_packed_attribute is too early to do this.  */
1760   for (variant = TYPE_NEXT_VARIANT (rli->t); variant;
1761        variant = TYPE_NEXT_VARIANT (variant))
1762     TYPE_PACKED (variant) = TYPE_PACKED (rli->t);
1763
1764   /* Lay out any static members.  This is done now because their type
1765      may use the record's type.  */
1766   while (!VEC_empty (tree, rli->pending_statics))
1767     layout_decl (VEC_pop (tree, rli->pending_statics), 0);
1768
1769   /* Clean up.  */
1770   if (free_p)
1771     {
1772       VEC_free (tree, gc, rli->pending_statics);
1773       free (rli);
1774     }
1775 }
1776 \f
1777
1778 /* Finish processing a builtin RECORD_TYPE type TYPE.  It's name is
1779    NAME, its fields are chained in reverse on FIELDS.
1780
1781    If ALIGN_TYPE is non-null, it is given the same alignment as
1782    ALIGN_TYPE.  */
1783
1784 void
1785 finish_builtin_struct (tree type, const char *name, tree fields,
1786                        tree align_type)
1787 {
1788   tree tail, next;
1789
1790   for (tail = NULL_TREE; fields; tail = fields, fields = next)
1791     {
1792       DECL_FIELD_CONTEXT (fields) = type;
1793       next = DECL_CHAIN (fields);
1794       DECL_CHAIN (fields) = tail;
1795     }
1796   TYPE_FIELDS (type) = tail;
1797
1798   if (align_type)
1799     {
1800       TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
1801       TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
1802     }
1803
1804   layout_type (type);
1805 #if 0 /* not yet, should get fixed properly later */
1806   TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
1807 #else
1808   TYPE_NAME (type) = build_decl (BUILTINS_LOCATION,
1809                                  TYPE_DECL, get_identifier (name), type);
1810 #endif
1811   TYPE_STUB_DECL (type) = TYPE_NAME (type);
1812   layout_decl (TYPE_NAME (type), 0);
1813 }
1814
1815 /* Calculate the mode, size, and alignment for TYPE.
1816    For an array type, calculate the element separation as well.
1817    Record TYPE on the chain of permanent or temporary types
1818    so that dbxout will find out about it.
1819
1820    TYPE_SIZE of a type is nonzero if the type has been laid out already.
1821    layout_type does nothing on such a type.
1822
1823    If the type is incomplete, its TYPE_SIZE remains zero.  */
1824
1825 void
1826 layout_type (tree type)
1827 {
1828   gcc_assert (type);
1829
1830   if (type == error_mark_node)
1831     return;
1832
1833   /* Do nothing if type has been laid out before.  */
1834   if (TYPE_SIZE (type))
1835     return;
1836
1837   switch (TREE_CODE (type))
1838     {
1839     case LANG_TYPE:
1840       /* This kind of type is the responsibility
1841          of the language-specific code.  */
1842       gcc_unreachable ();
1843
1844     case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill.  */
1845       if (TYPE_PRECISION (type) == 0)
1846         TYPE_PRECISION (type) = 1; /* default to one byte/boolean.  */
1847
1848       /* ... fall through ...  */
1849
1850     case INTEGER_TYPE:
1851     case ENUMERAL_TYPE:
1852       if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1853           && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1854         TYPE_UNSIGNED (type) = 1;
1855
1856       SET_TYPE_MODE (type,
1857                      smallest_mode_for_size (TYPE_PRECISION (type), MODE_INT));
1858       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1859       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1860       break;
1861
1862     case REAL_TYPE:
1863       SET_TYPE_MODE (type,
1864                      mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0));
1865       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1866       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1867       break;
1868
1869    case FIXED_POINT_TYPE:
1870      /* TYPE_MODE (type) has been set already.  */
1871      TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1872      TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1873      break;
1874
1875     case COMPLEX_TYPE:
1876       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1877       SET_TYPE_MODE (type,
1878                      mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1879                                     (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
1880                                      ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
1881                                      0));
1882       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1883       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1884       break;
1885
1886     case VECTOR_TYPE:
1887       {
1888         int nunits = TYPE_VECTOR_SUBPARTS (type);
1889         tree innertype = TREE_TYPE (type);
1890
1891         gcc_assert (!(nunits & (nunits - 1)));
1892
1893         /* Find an appropriate mode for the vector type.  */
1894         if (TYPE_MODE (type) == VOIDmode)
1895           SET_TYPE_MODE (type,
1896                          mode_for_vector (TYPE_MODE (innertype), nunits));
1897
1898         TYPE_SATURATING (type) = TYPE_SATURATING (TREE_TYPE (type));
1899         TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1900         TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
1901                                                  TYPE_SIZE_UNIT (innertype),
1902                                                  size_int (nunits), 0);
1903         TYPE_SIZE (type) = int_const_binop (MULT_EXPR, TYPE_SIZE (innertype),
1904                                             bitsize_int (nunits), 0);
1905
1906         /* Always naturally align vectors.  This prevents ABI changes
1907            depending on whether or not native vector modes are supported.  */
1908         TYPE_ALIGN (type) = tree_low_cst (TYPE_SIZE (type), 0);
1909         break;
1910       }
1911
1912     case VOID_TYPE:
1913       /* This is an incomplete type and so doesn't have a size.  */
1914       TYPE_ALIGN (type) = 1;
1915       TYPE_USER_ALIGN (type) = 0;
1916       SET_TYPE_MODE (type, VOIDmode);
1917       break;
1918
1919     case OFFSET_TYPE:
1920       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1921       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1922       /* A pointer might be MODE_PARTIAL_INT,
1923          but ptrdiff_t must be integral.  */
1924       SET_TYPE_MODE (type, mode_for_size (POINTER_SIZE, MODE_INT, 0));
1925       TYPE_PRECISION (type) = POINTER_SIZE;
1926       break;
1927
1928     case FUNCTION_TYPE:
1929     case METHOD_TYPE:
1930       /* It's hard to see what the mode and size of a function ought to
1931          be, but we do know the alignment is FUNCTION_BOUNDARY, so
1932          make it consistent with that.  */
1933       SET_TYPE_MODE (type, mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0));
1934       TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
1935       TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1936       break;
1937
1938     case POINTER_TYPE:
1939     case REFERENCE_TYPE:
1940       {
1941         enum machine_mode mode = TYPE_MODE (type);
1942         if (TREE_CODE (type) == REFERENCE_TYPE && reference_types_internal)
1943           {
1944             addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1945             mode = targetm.addr_space.address_mode (as);
1946           }
1947
1948         TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
1949         TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
1950         TYPE_UNSIGNED (type) = 1;
1951         TYPE_PRECISION (type) = GET_MODE_BITSIZE (mode);
1952       }
1953       break;
1954
1955     case ARRAY_TYPE:
1956       {
1957         tree index = TYPE_DOMAIN (type);
1958         tree element = TREE_TYPE (type);
1959
1960         build_pointer_type (element);
1961
1962         /* We need to know both bounds in order to compute the size.  */
1963         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1964             && TYPE_SIZE (element))
1965           {
1966             tree ub = TYPE_MAX_VALUE (index);
1967             tree lb = TYPE_MIN_VALUE (index);
1968             tree element_size = TYPE_SIZE (element);
1969             tree length;
1970
1971             /* Make sure that an array of zero-sized element is zero-sized
1972                regardless of its extent.  */
1973             if (integer_zerop (element_size))
1974               length = size_zero_node;
1975
1976             /* The initial subtraction should happen in the original type so
1977                that (possible) negative values are handled appropriately.  */
1978             else
1979               length
1980                 = size_binop (PLUS_EXPR, size_one_node,
1981                               fold_convert (sizetype,
1982                                             fold_build2_loc (input_location,
1983                                                              MINUS_EXPR,
1984                                                              TREE_TYPE (lb),
1985                                                              ub, lb)));
1986
1987             TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1988                                            fold_convert (bitsizetype,
1989                                                          length));
1990
1991             /* If we know the size of the element, calculate the total size
1992                directly, rather than do some division thing below.  This
1993                optimization helps Fortran assumed-size arrays (where the
1994                size of the array is determined at runtime) substantially.  */
1995             if (TYPE_SIZE_UNIT (element))
1996               TYPE_SIZE_UNIT (type)
1997                 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1998           }
1999
2000         /* Now round the alignment and size,
2001            using machine-dependent criteria if any.  */
2002
2003 #ifdef ROUND_TYPE_ALIGN
2004         TYPE_ALIGN (type)
2005           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
2006 #else
2007         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
2008 #endif
2009         if (!TYPE_SIZE (element))
2010           /* We don't know the size of the underlying element type, so
2011              our alignment calculations will be wrong, forcing us to
2012              fall back on structural equality. */
2013           SET_TYPE_STRUCTURAL_EQUALITY (type);
2014         TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
2015         SET_TYPE_MODE (type, BLKmode);
2016         if (TYPE_SIZE (type) != 0
2017 #ifdef MEMBER_TYPE_FORCES_BLK
2018             && ! MEMBER_TYPE_FORCES_BLK (type, VOIDmode)
2019 #endif
2020             /* BLKmode elements force BLKmode aggregate;
2021                else extract/store fields may lose.  */
2022             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
2023                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
2024           {
2025             /* One-element arrays get the component type's mode.  */
2026             if (simple_cst_equal (TYPE_SIZE (type),
2027                                   TYPE_SIZE (TREE_TYPE (type))))
2028               SET_TYPE_MODE (type, TYPE_MODE (TREE_TYPE (type)));
2029             else
2030               SET_TYPE_MODE (type, mode_for_size_tree (TYPE_SIZE (type),
2031                                                        MODE_INT, 1));
2032
2033             if (TYPE_MODE (type) != BLKmode
2034                 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
2035                 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
2036               {
2037                 TYPE_NO_FORCE_BLK (type) = 1;
2038                 SET_TYPE_MODE (type, BLKmode);
2039               }
2040           }
2041         /* When the element size is constant, check that it is at least as
2042            large as the element alignment.  */
2043         if (TYPE_SIZE_UNIT (element)
2044             && TREE_CODE (TYPE_SIZE_UNIT (element)) == INTEGER_CST
2045             /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
2046                TYPE_ALIGN_UNIT.  */
2047             && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element))
2048             && !integer_zerop (TYPE_SIZE_UNIT (element))
2049             && compare_tree_int (TYPE_SIZE_UNIT (element),
2050                                  TYPE_ALIGN_UNIT (element)) < 0)
2051           error ("alignment of array elements is greater than element size");
2052         break;
2053       }
2054
2055     case RECORD_TYPE:
2056     case UNION_TYPE:
2057     case QUAL_UNION_TYPE:
2058       {
2059         tree field;
2060         record_layout_info rli;
2061
2062         /* Initialize the layout information.  */
2063         rli = start_record_layout (type);
2064
2065         /* If this is a QUAL_UNION_TYPE, we want to process the fields
2066            in the reverse order in building the COND_EXPR that denotes
2067            its size.  We reverse them again later.  */
2068         if (TREE_CODE (type) == QUAL_UNION_TYPE)
2069           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2070
2071         /* Place all the fields.  */
2072         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2073           place_field (rli, field);
2074
2075         if (TREE_CODE (type) == QUAL_UNION_TYPE)
2076           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2077
2078         /* Finish laying out the record.  */
2079         finish_record_layout (rli, /*free_p=*/true);
2080       }
2081       break;
2082
2083     default:
2084       gcc_unreachable ();
2085     }
2086
2087   /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE.  For
2088      records and unions, finish_record_layout already called this
2089      function.  */
2090   if (TREE_CODE (type) != RECORD_TYPE
2091       && TREE_CODE (type) != UNION_TYPE
2092       && TREE_CODE (type) != QUAL_UNION_TYPE)
2093     finalize_type_size (type);
2094
2095   /* We should never see alias sets on incomplete aggregates.  And we
2096      should not call layout_type on not incomplete aggregates.  */
2097   if (AGGREGATE_TYPE_P (type))
2098     gcc_assert (!TYPE_ALIAS_SET_KNOWN_P (type));
2099 }
2100
2101 /* Vector types need to re-check the target flags each time we report
2102    the machine mode.  We need to do this because attribute target can
2103    change the result of vector_mode_supported_p and have_regs_of_mode
2104    on a per-function basis.  Thus the TYPE_MODE of a VECTOR_TYPE can
2105    change on a per-function basis.  */
2106 /* ??? Possibly a better solution is to run through all the types
2107    referenced by a function and re-compute the TYPE_MODE once, rather
2108    than make the TYPE_MODE macro call a function.  */
2109
2110 enum machine_mode
2111 vector_type_mode (const_tree t)
2112 {
2113   enum machine_mode mode;
2114
2115   gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
2116
2117   mode = t->type.mode;
2118   if (VECTOR_MODE_P (mode)
2119       && (!targetm.vector_mode_supported_p (mode)
2120           || !have_regs_of_mode[mode]))
2121     {
2122       enum machine_mode innermode = TREE_TYPE (t)->type.mode;
2123
2124       /* For integers, try mapping it to a same-sized scalar mode.  */
2125       if (GET_MODE_CLASS (innermode) == MODE_INT)
2126         {
2127           mode = mode_for_size (TYPE_VECTOR_SUBPARTS (t)
2128                                 * GET_MODE_BITSIZE (innermode), MODE_INT, 0);
2129
2130           if (mode != VOIDmode && have_regs_of_mode[mode])
2131             return mode;
2132         }
2133
2134       return BLKmode;
2135     }
2136
2137   return mode;
2138 }
2139 \f
2140 /* Create and return a type for signed integers of PRECISION bits.  */
2141
2142 tree
2143 make_signed_type (int precision)
2144 {
2145   tree type = make_node (INTEGER_TYPE);
2146
2147   TYPE_PRECISION (type) = precision;
2148
2149   fixup_signed_type (type);
2150   return type;
2151 }
2152
2153 /* Create and return a type for unsigned integers of PRECISION bits.  */
2154
2155 tree
2156 make_unsigned_type (int precision)
2157 {
2158   tree type = make_node (INTEGER_TYPE);
2159
2160   TYPE_PRECISION (type) = precision;
2161
2162   fixup_unsigned_type (type);
2163   return type;
2164 }
2165 \f
2166 /* Create and return a type for fract of PRECISION bits, UNSIGNEDP,
2167    and SATP.  */
2168
2169 tree
2170 make_fract_type (int precision, int unsignedp, int satp)
2171 {
2172   tree type = make_node (FIXED_POINT_TYPE);
2173
2174   TYPE_PRECISION (type) = precision;
2175
2176   if (satp)
2177     TYPE_SATURATING (type) = 1;
2178
2179   /* Lay out the type: set its alignment, size, etc.  */
2180   if (unsignedp)
2181     {
2182       TYPE_UNSIGNED (type) = 1;
2183       SET_TYPE_MODE (type, mode_for_size (precision, MODE_UFRACT, 0));
2184     }
2185   else
2186     SET_TYPE_MODE (type, mode_for_size (precision, MODE_FRACT, 0));
2187   layout_type (type);
2188
2189   return type;
2190 }
2191
2192 /* Create and return a type for accum of PRECISION bits, UNSIGNEDP,
2193    and SATP.  */
2194
2195 tree
2196 make_accum_type (int precision, int unsignedp, int satp)
2197 {
2198   tree type = make_node (FIXED_POINT_TYPE);
2199
2200   TYPE_PRECISION (type) = precision;
2201
2202   if (satp)
2203     TYPE_SATURATING (type) = 1;
2204
2205   /* Lay out the type: set its alignment, size, etc.  */
2206   if (unsignedp)
2207     {
2208       TYPE_UNSIGNED (type) = 1;
2209       SET_TYPE_MODE (type, mode_for_size (precision, MODE_UACCUM, 0));
2210     }
2211   else
2212     SET_TYPE_MODE (type, mode_for_size (precision, MODE_ACCUM, 0));
2213   layout_type (type);
2214
2215   return type;
2216 }
2217
2218 /* Initialize sizetype and bitsizetype to a reasonable and temporary
2219    value to enable integer types to be created.  */
2220
2221 void
2222 initialize_sizetypes (void)
2223 {
2224   tree t = make_node (INTEGER_TYPE);
2225   int precision = GET_MODE_BITSIZE (SImode);
2226
2227   SET_TYPE_MODE (t, SImode);
2228   TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
2229   TYPE_IS_SIZETYPE (t) = 1;
2230   TYPE_UNSIGNED (t) = 1;
2231   TYPE_SIZE (t) = build_int_cst (t, precision);
2232   TYPE_SIZE_UNIT (t) = build_int_cst (t, GET_MODE_SIZE (SImode));
2233   TYPE_PRECISION (t) = precision;
2234
2235   set_min_and_max_values_for_integral_type (t, precision, true);
2236
2237   sizetype = t;
2238   bitsizetype = build_distinct_type_copy (t);
2239 }
2240
2241 /* Make sizetype a version of TYPE, and initialize *sizetype accordingly.
2242    We do this by overwriting the stub sizetype and bitsizetype nodes created
2243    by initialize_sizetypes.  This makes sure that (a) anything stubby about
2244    them no longer exists and (b) any INTEGER_CSTs created with such a type,
2245    remain valid.  */
2246
2247 void
2248 set_sizetype (tree type)
2249 {
2250   tree t, max;
2251   int oprecision = TYPE_PRECISION (type);
2252   /* The *bitsizetype types use a precision that avoids overflows when
2253      calculating signed sizes / offsets in bits.  However, when
2254      cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
2255      precision.  */
2256   int precision
2257     = MIN (oprecision + BITS_PER_UNIT_LOG + 1, MAX_FIXED_MODE_SIZE);
2258   precision
2259     = GET_MODE_PRECISION (smallest_mode_for_size (precision, MODE_INT));
2260   if (precision > HOST_BITS_PER_WIDE_INT * 2)
2261     precision = HOST_BITS_PER_WIDE_INT * 2;
2262
2263   /* sizetype must be an unsigned type.  */
2264   gcc_assert (TYPE_UNSIGNED (type));
2265
2266   t = build_distinct_type_copy (type);
2267   /* We want to use sizetype's cache, as we will be replacing that type.  */
2268   TYPE_CACHED_VALUES (t) = TYPE_CACHED_VALUES (sizetype);
2269   TYPE_CACHED_VALUES_P (t) = TYPE_CACHED_VALUES_P (sizetype);
2270   TREE_TYPE (TYPE_CACHED_VALUES (t)) = type;
2271   TYPE_UID (t) = TYPE_UID (sizetype);
2272   TYPE_IS_SIZETYPE (t) = 1;
2273
2274   /* Replace our original stub sizetype.  */
2275   memcpy (sizetype, t, tree_size (sizetype));
2276   TYPE_MAIN_VARIANT (sizetype) = sizetype;
2277   TYPE_CANONICAL (sizetype) = sizetype;
2278
2279   /* sizetype is unsigned but we need to fix TYPE_MAX_VALUE so that it is
2280      sign-extended in a way consistent with force_fit_type.  */
2281   max = TYPE_MAX_VALUE (sizetype);
2282   TYPE_MAX_VALUE (sizetype)
2283     = double_int_to_tree (sizetype, tree_to_double_int (max));
2284
2285   t = make_node (INTEGER_TYPE);
2286   TYPE_NAME (t) = get_identifier ("bit_size_type");
2287   /* We want to use bitsizetype's cache, as we will be replacing that type.  */
2288   TYPE_CACHED_VALUES (t) = TYPE_CACHED_VALUES (bitsizetype);
2289   TYPE_CACHED_VALUES_P (t) = TYPE_CACHED_VALUES_P (bitsizetype);
2290   TYPE_PRECISION (t) = precision;
2291   TYPE_UID (t) = TYPE_UID (bitsizetype);
2292   TYPE_IS_SIZETYPE (t) = 1;
2293
2294   /* Replace our original stub bitsizetype.  */
2295   memcpy (bitsizetype, t, tree_size (bitsizetype));
2296   TYPE_MAIN_VARIANT (bitsizetype) = bitsizetype;
2297   TYPE_CANONICAL (bitsizetype) = bitsizetype;
2298
2299   fixup_unsigned_type (bitsizetype);
2300
2301   /* Create the signed variants of *sizetype.  */
2302   ssizetype = make_signed_type (oprecision);
2303   TYPE_IS_SIZETYPE (ssizetype) = 1;
2304   sbitsizetype = make_signed_type (precision);
2305   TYPE_IS_SIZETYPE (sbitsizetype) = 1;
2306 }
2307 \f
2308 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE
2309    or BOOLEAN_TYPE.  Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
2310    for TYPE, based on the PRECISION and whether or not the TYPE
2311    IS_UNSIGNED.  PRECISION need not correspond to a width supported
2312    natively by the hardware; for example, on a machine with 8-bit,
2313    16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
2314    61.  */
2315
2316 void
2317 set_min_and_max_values_for_integral_type (tree type,
2318                                           int precision,
2319                                           bool is_unsigned)
2320 {
2321   tree min_value;
2322   tree max_value;
2323
2324   if (is_unsigned)
2325     {
2326       min_value = build_int_cst (type, 0);
2327       max_value
2328         = build_int_cst_wide (type, precision - HOST_BITS_PER_WIDE_INT >= 0
2329                               ? -1
2330                               : ((HOST_WIDE_INT) 1 << precision) - 1,
2331                               precision - HOST_BITS_PER_WIDE_INT > 0
2332                               ? ((unsigned HOST_WIDE_INT) ~0
2333                                  >> (HOST_BITS_PER_WIDE_INT
2334                                      - (precision - HOST_BITS_PER_WIDE_INT)))
2335                               : 0);
2336     }
2337   else
2338     {
2339       min_value
2340         = build_int_cst_wide (type,
2341                               (precision - HOST_BITS_PER_WIDE_INT > 0
2342                                ? 0
2343                                : (HOST_WIDE_INT) (-1) << (precision - 1)),
2344                               (((HOST_WIDE_INT) (-1)
2345                                 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
2346                                     ? precision - HOST_BITS_PER_WIDE_INT - 1
2347                                     : 0))));
2348       max_value
2349         = build_int_cst_wide (type,
2350                               (precision - HOST_BITS_PER_WIDE_INT > 0
2351                                ? -1
2352                                : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
2353                               (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
2354                                ? (((HOST_WIDE_INT) 1
2355                                    << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
2356                                : 0));
2357     }
2358
2359   TYPE_MIN_VALUE (type) = min_value;
2360   TYPE_MAX_VALUE (type) = max_value;
2361 }
2362
2363 /* Set the extreme values of TYPE based on its precision in bits,
2364    then lay it out.  Used when make_signed_type won't do
2365    because the tree code is not INTEGER_TYPE.
2366    E.g. for Pascal, when the -fsigned-char option is given.  */
2367
2368 void
2369 fixup_signed_type (tree type)
2370 {
2371   int precision = TYPE_PRECISION (type);
2372
2373   /* We can not represent properly constants greater then
2374      2 * HOST_BITS_PER_WIDE_INT, still we need the types
2375      as they are used by i386 vector extensions and friends.  */
2376   if (precision > HOST_BITS_PER_WIDE_INT * 2)
2377     precision = HOST_BITS_PER_WIDE_INT * 2;
2378
2379   set_min_and_max_values_for_integral_type (type, precision,
2380                                             /*is_unsigned=*/false);
2381
2382   /* Lay out the type: set its alignment, size, etc.  */
2383   layout_type (type);
2384 }
2385
2386 /* Set the extreme values of TYPE based on its precision in bits,
2387    then lay it out.  This is used both in `make_unsigned_type'
2388    and for enumeral types.  */
2389
2390 void
2391 fixup_unsigned_type (tree type)
2392 {
2393   int precision = TYPE_PRECISION (type);
2394
2395   /* We can not represent properly constants greater then
2396      2 * HOST_BITS_PER_WIDE_INT, still we need the types
2397      as they are used by i386 vector extensions and friends.  */
2398   if (precision > HOST_BITS_PER_WIDE_INT * 2)
2399     precision = HOST_BITS_PER_WIDE_INT * 2;
2400
2401   TYPE_UNSIGNED (type) = 1;
2402
2403   set_min_and_max_values_for_integral_type (type, precision,
2404                                             /*is_unsigned=*/true);
2405
2406   /* Lay out the type: set its alignment, size, etc.  */
2407   layout_type (type);
2408 }
2409 \f
2410 /* Find the best machine mode to use when referencing a bit field of length
2411    BITSIZE bits starting at BITPOS.
2412
2413    The underlying object is known to be aligned to a boundary of ALIGN bits.
2414    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2415    larger than LARGEST_MODE (usually SImode).
2416
2417    If no mode meets all these conditions, we return VOIDmode.
2418
2419    If VOLATILEP is false and SLOW_BYTE_ACCESS is false, we return the
2420    smallest mode meeting these conditions.
2421
2422    If VOLATILEP is false and SLOW_BYTE_ACCESS is true, we return the
2423    largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2424    all the conditions.
2425
2426    If VOLATILEP is true the narrow_volatile_bitfields target hook is used to
2427    decide which of the above modes should be used.  */
2428
2429 enum machine_mode
2430 get_best_mode (int bitsize, int bitpos, unsigned int align,
2431                enum machine_mode largest_mode, int volatilep)
2432 {
2433   enum machine_mode mode;
2434   unsigned int unit = 0;
2435
2436   /* Find the narrowest integer mode that contains the bit field.  */
2437   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2438        mode = GET_MODE_WIDER_MODE (mode))
2439     {
2440       unit = GET_MODE_BITSIZE (mode);
2441       if ((bitpos % unit) + bitsize <= unit)
2442         break;
2443     }
2444
2445   if (mode == VOIDmode
2446       /* It is tempting to omit the following line
2447          if STRICT_ALIGNMENT is true.
2448          But that is incorrect, since if the bitfield uses part of 3 bytes
2449          and we use a 4-byte mode, we could get a spurious segv
2450          if the extra 4th byte is past the end of memory.
2451          (Though at least one Unix compiler ignores this problem:
2452          that on the Sequent 386 machine.  */
2453       || MIN (unit, BIGGEST_ALIGNMENT) > align
2454       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
2455     return VOIDmode;
2456
2457   if ((SLOW_BYTE_ACCESS && ! volatilep)
2458       || (volatilep && !targetm.narrow_volatile_bitfield ()))
2459     {
2460       enum machine_mode wide_mode = VOIDmode, tmode;
2461
2462       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
2463            tmode = GET_MODE_WIDER_MODE (tmode))
2464         {
2465           unit = GET_MODE_BITSIZE (tmode);
2466           if (bitpos / unit == (bitpos + bitsize - 1) / unit
2467               && unit <= BITS_PER_WORD
2468               && unit <= MIN (align, BIGGEST_ALIGNMENT)
2469               && (largest_mode == VOIDmode
2470                   || unit <= GET_MODE_BITSIZE (largest_mode)))
2471             wide_mode = tmode;
2472         }
2473
2474       if (wide_mode != VOIDmode)
2475         return wide_mode;
2476     }
2477
2478   return mode;
2479 }
2480
2481 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2482    SIGN).  The returned constants are made to be usable in TARGET_MODE.  */
2483
2484 void
2485 get_mode_bounds (enum machine_mode mode, int sign,
2486                  enum machine_mode target_mode,
2487                  rtx *mmin, rtx *mmax)
2488 {
2489   unsigned size = GET_MODE_BITSIZE (mode);
2490   unsigned HOST_WIDE_INT min_val, max_val;
2491
2492   gcc_assert (size <= HOST_BITS_PER_WIDE_INT);
2493
2494   if (sign)
2495     {
2496       min_val = -((unsigned HOST_WIDE_INT) 1 << (size - 1));
2497       max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1)) - 1;
2498     }
2499   else
2500     {
2501       min_val = 0;
2502       max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1) << 1) - 1;
2503     }
2504
2505   *mmin = gen_int_mode (min_val, target_mode);
2506   *mmax = gen_int_mode (max_val, target_mode);
2507 }
2508
2509 #include "gt-stor-layout.h"