OSDN Git Service

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