OSDN Git Service

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