OSDN Git Service

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