OSDN Git Service

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