1/* C-compiler utilities for types and variables storage layout
2   Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING.  If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.  */
20
21
22#include "config.h"
23#include "system.h"
24
25#include "tree.h"
26#include "rtl.h"
27#include "flags.h"
28#include "except.h"
29#include "function.h"
30#include "expr.h"
31#include "toplev.h"
32
33#define CEIL(x,y) (((x) + (y) - 1) / (y))
34
35/* Data type for the expressions representing sizes of data types.
36   It is the first integer type laid out.  */
37
38struct sizetype_tab sizetype_tab;
39
40/* An integer constant with value 0 whose type is sizetype.  */
41
42tree size_zero_node;
43
44/* An integer constant with value 1 whose type is sizetype.  */
45
46tree size_one_node;
47
48/* If nonzero, this is an upper limit on alignment of structure fields.
49   The value is measured in bits.  */
50int maximum_field_alignment;
51
52/* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
53   May be overridden by front-ends.  */
54int set_alignment = 0;
55
56static tree layout_record	PROTO((tree));
57static void layout_union	PROTO((tree));
58
59/* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
60
61static tree pending_sizes;
62
63/* Nonzero means cannot safely call expand_expr now,
64   so put variable sizes onto `pending_sizes' instead.  */
65
66int immediate_size_expand;
67
68tree
69get_pending_sizes ()
70{
71  tree chain = pending_sizes;
72  tree t;
73
74  /* Put each SAVE_EXPR into the current function.  */
75  for (t = chain; t; t = TREE_CHAIN (t))
76    SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
77  pending_sizes = 0;
78  return chain;
79}
80
81void
82put_pending_sizes (chain)
83     tree chain;
84{
85  if (pending_sizes)
86    abort ();
87
88  pending_sizes = chain;
89}
90
91/* Given a size SIZE that may not be a constant, return a SAVE_EXPR
92   to serve as the actual size-expression for a type or decl.  */
93
94tree
95variable_size (size)
96     tree size;
97{
98  /* If the language-processor is to take responsibility for variable-sized
99     items (e.g., languages which have elaboration procedures like Ada),
100     just return SIZE unchanged.  Likewise for self-referential sizes.  */
101  if (TREE_CONSTANT (size)
102      || global_bindings_p () < 0 || contains_placeholder_p (size))
103    return size;
104
105  size = save_expr (size);
106
107  if (global_bindings_p ())
108    {
109      if (TREE_CONSTANT (size))
110	error ("type size can't be explicitly evaluated");
111      else
112	error ("variable-size type declared outside of any function");
113
114      return size_int (1);
115    }
116
117  if (immediate_size_expand)
118    /* NULL_RTX is not defined; neither is the rtx type.
119       Also, we would like to pass const0_rtx here, but don't have it.  */
120    expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
121		 VOIDmode, 0);
122  else
123    pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
124
125  return size;
126}
127
128#ifndef MAX_FIXED_MODE_SIZE
129#define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
130#endif
131
132/* Return the machine mode to use for a nonscalar of SIZE bits.
133   The mode must be in class CLASS, and have exactly that many bits.
134   If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
135   be used.  */
136
137enum machine_mode
138mode_for_size (size, class, limit)
139     unsigned int size;
140     enum mode_class class;
141     int limit;
142{
143  register enum machine_mode mode;
144
145  if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
146    return BLKmode;
147
148  /* Get the first mode which has this size, in the specified class.  */
149  for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
150       mode = GET_MODE_WIDER_MODE (mode))
151    if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
152      return mode;
153
154  return BLKmode;
155}
156
157/* Similar, but never return BLKmode; return the narrowest mode that
158   contains at least the requested number of bits.  */
159
160enum machine_mode
161smallest_mode_for_size (size, class)
162     unsigned int size;
163     enum mode_class class;
164{
165  register enum machine_mode mode;
166
167  /* Get the first mode which has at least this size, in the
168     specified class.  */
169  for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
170       mode = GET_MODE_WIDER_MODE (mode))
171    if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
172      return mode;
173
174  abort ();
175}
176
177/* Find an integer mode of the exact same size, or BLKmode on failure.  */
178
179enum machine_mode
180int_mode_for_mode (mode)
181     enum machine_mode mode;
182{
183  switch (GET_MODE_CLASS (mode))
184    {
185    case MODE_INT:
186    case MODE_PARTIAL_INT:
187      break;
188
189    case MODE_COMPLEX_INT:
190    case MODE_COMPLEX_FLOAT:
191    case MODE_FLOAT:
192      mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
193      break;
194
195    case MODE_RANDOM:
196      if (mode == BLKmode)
197        break;
198      /* FALLTHRU */
199
200    case MODE_CC:
201    default:
202      abort();
203    }
204
205  return mode;
206}
207
208/* Return the value of VALUE, rounded up to a multiple of DIVISOR.  */
209
210tree
211round_up (value, divisor)
212     tree value;
213     int divisor;
214{
215  return size_binop (MULT_EXPR,
216		     size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
217		     size_int (divisor));
218}
219
220/* Set the size, mode and alignment of a ..._DECL node.
221   TYPE_DECL does need this for C++.
222   Note that LABEL_DECL and CONST_DECL nodes do not need this,
223   and FUNCTION_DECL nodes have them set up in a special (and simple) way.
224   Don't call layout_decl for them.
225
226   KNOWN_ALIGN is the amount of alignment we can assume this
227   decl has with no special effort.  It is relevant only for FIELD_DECLs
228   and depends on the previous fields.
229   All that matters about KNOWN_ALIGN is which powers of 2 divide it.
230   If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
231   the record will be aligned to suit.  */
232
233void
234layout_decl (decl, known_align)
235     tree decl;
236     unsigned known_align;
237{
238  register tree type = TREE_TYPE (decl);
239  register enum tree_code code = TREE_CODE (decl);
240  int spec_size = DECL_FIELD_SIZE (decl);
241
242  if (code == CONST_DECL)
243    return;
244
245  if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
246      && code != FIELD_DECL && code != TYPE_DECL)
247    abort ();
248
249  if (type == error_mark_node)
250    {
251      type = void_type_node;
252      spec_size = 0;
253    }
254
255  /* Usually the size and mode come from the data type without change.  */
256
257  DECL_MODE (decl) = TYPE_MODE (type);
258  TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
259  if (DECL_SIZE (decl) == 0)
260    DECL_SIZE (decl) = TYPE_SIZE (type);
261
262  if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
263    {
264      if (spec_size == 0 && DECL_NAME (decl) != 0)
265	abort ();
266
267      /* Size is specified number of bits.  */
268      DECL_SIZE (decl) = size_int (spec_size);
269    }
270  /* Force alignment required for the data type.
271     But if the decl itself wants greater alignment, don't override that.
272     Likewise, if the decl is packed, don't override it.  */
273  else if (DECL_ALIGN (decl) == 0
274	   || (! DECL_PACKED (decl) &&  TYPE_ALIGN (type) > DECL_ALIGN (decl)))
275    DECL_ALIGN (decl) = TYPE_ALIGN (type);
276
277  /* See if we can use an ordinary integer mode for a bit-field.  */
278  /* Conditions are: a fixed size that is correct for another mode
279     and occupying a complete byte or bytes on proper boundary.  */
280  if (code == FIELD_DECL)
281    {
282      DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
283      if (maximum_field_alignment != 0)
284	DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl),
285				 (unsigned)maximum_field_alignment);
286      else if (DECL_PACKED (decl))
287	DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
288    }
289
290  if (DECL_BIT_FIELD (decl)
291      && TYPE_SIZE (type) != 0
292      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
293      && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
294    {
295      register enum machine_mode xmode
296	= mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
297
298      if (xmode != BLKmode
299	  && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
300	{
301	  DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode),
302				   DECL_ALIGN (decl));
303	  DECL_MODE (decl) = xmode;
304	  DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
305	  /* This no longer needs to be accessed as a bit field.  */
306	  DECL_BIT_FIELD (decl) = 0;
307	}
308    }
309
310  /* Turn off DECL_BIT_FIELD if we won't need it set.  */
311  if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
312      && known_align % TYPE_ALIGN (type) == 0
313      && DECL_SIZE (decl) != 0
314      && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
315	  || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
316      && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
317    DECL_BIT_FIELD (decl) = 0;
318
319  /* Evaluate nonconstant size only once, either now or as soon as safe.  */
320  if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
321    DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
322}
323
324/* Lay out a RECORD_TYPE type (a C struct).
325   This means laying out the fields, determining their positions,
326   and computing the overall size and required alignment of the record.
327   Note that if you set the TYPE_ALIGN before calling this
328   then the struct is aligned to at least that boundary.
329
330   If the type has basetypes, you must call layout_basetypes
331   before calling this function.
332
333   The return value is a list of static members of the record.
334   They still need to be laid out.  */
335
336static tree
337layout_record (rec)
338     tree rec;
339{
340  register tree field;
341  unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
342  /* These must be laid out *after* the record is.  */
343  tree pending_statics = NULL_TREE;
344  /* Record size so far is CONST_SIZE + VAR_SIZE bits,
345     where CONST_SIZE is an integer
346     and VAR_SIZE is a tree expression.
347     If VAR_SIZE is null, the size is just CONST_SIZE.
348     Naturally we try to avoid using VAR_SIZE.  */
349 register HOST_WIDE_INT const_size = 0;
350  register tree var_size = 0;
351  /* Once we start using VAR_SIZE, this is the maximum alignment
352     that we know VAR_SIZE has.  */
353  register int var_align = BITS_PER_UNIT;
354
355#ifdef STRUCTURE_SIZE_BOUNDARY
356  /* Packed structures don't need to have minimum size.  */
357  if (! TYPE_PACKED (rec))
358    record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
359#endif
360
361  for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
362    {
363      register int known_align = var_size ? var_align : const_size;
364      register int desired_align = 0;
365
366      /* If FIELD is static, then treat it like a separate variable,
367	 not really like a structure field.
368	 If it is a FUNCTION_DECL, it's a method.
369	 In both cases, all we do is lay out the decl,
370	 and we do it *after* the record is laid out.  */
371
372      if (TREE_CODE (field) == VAR_DECL)
373	{
374	  pending_statics = tree_cons (NULL_TREE, field, pending_statics);
375	  continue;
376	}
377      /* Enumerators and enum types which are local to this class need not
378	 be laid out.  Likewise for initialized constant fields.  */
379      if (TREE_CODE (field) != FIELD_DECL)
380	continue;
381
382      /* Lay out the field so we know what alignment it needs.
383	 For a packed field, use the alignment as specified,
384	 disregarding what the type would want.  */
385      if (DECL_PACKED (field))
386	desired_align = DECL_ALIGN (field);
387      layout_decl (field, known_align);
388      if (! DECL_PACKED (field))
389	desired_align = DECL_ALIGN (field);
390      /* Some targets (i.e. VMS) limit struct field alignment
391	 to a lower boundary than alignment of variables.  */
392#ifdef BIGGEST_FIELD_ALIGNMENT
393      desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
394#endif
395#ifdef ADJUST_FIELD_ALIGN
396      desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
397#endif
398
399      /* Record must have at least as much alignment as any field.
400	 Otherwise, the alignment of the field within the record
401	 is meaningless.  */
402
403#ifndef PCC_BITFIELD_TYPE_MATTERS
404      record_align = MAX (record_align, desired_align);
405#else
406      if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
407	  && DECL_BIT_FIELD_TYPE (field)
408	  && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
409	{
410	  /* For these machines, a zero-length field does not
411	     affect the alignment of the structure as a whole.
412	     It does, however, affect the alignment of the next field
413	     within the structure.  */
414	  if (! integer_zerop (DECL_SIZE (field)))
415	    record_align = MAX ((int)record_align, desired_align);
416	  else if (! DECL_PACKED (field))
417	    desired_align = TYPE_ALIGN (TREE_TYPE (field));
418	  /* A named bit field of declared type `int'
419	     forces the entire structure to have `int' alignment.  */
420	  if (DECL_NAME (field) != 0)
421	    {
422	      int type_align = TYPE_ALIGN (TREE_TYPE (field));
423	      if (maximum_field_alignment != 0)
424		type_align = MIN (type_align, maximum_field_alignment);
425	      else if (DECL_PACKED (field))
426		type_align = MIN (type_align, BITS_PER_UNIT);
427
428	      record_align = MAX ((int)record_align, type_align);
429	    }
430	}
431      else
432	record_align = MAX ((int)record_align, desired_align);
433#endif
434
435      /* Does this field automatically have alignment it needs
436	 by virtue of the fields that precede it and the record's
437	 own alignment?  */
438
439      if (const_size % desired_align != 0
440	  || (var_align % desired_align != 0
441	      && var_size != 0))
442	{
443	  /* No, we need to skip space before this field.
444	     Bump the cumulative size to multiple of field alignment.  */
445
446	  if (var_size == 0
447	      || var_align % desired_align == 0)
448	    const_size
449	      = CEIL (const_size, desired_align) * desired_align;
450	  else
451	    {
452	      if (const_size > 0)
453		var_size = size_binop (PLUS_EXPR, var_size,
454				       bitsize_int (const_size, 0L));
455	      const_size = 0;
456	      var_size = round_up (var_size, desired_align);
457	      var_align = MIN (var_align, desired_align);
458	    }
459	}
460
461#ifdef PCC_BITFIELD_TYPE_MATTERS
462      if (PCC_BITFIELD_TYPE_MATTERS
463	  && TREE_CODE (field) == FIELD_DECL
464	  && TREE_TYPE (field) != error_mark_node
465	  && DECL_BIT_FIELD_TYPE (field)
466	  && !DECL_PACKED (field)
467	  && maximum_field_alignment == 0
468	  && !integer_zerop (DECL_SIZE (field)))
469	{
470	  int type_align = TYPE_ALIGN (TREE_TYPE (field));
471	  register tree dsize = DECL_SIZE (field);
472	  int field_size = TREE_INT_CST_LOW (dsize);
473
474	  /* A bit field may not span more units of alignment of its type
475	     than its type itself.  Advance to next boundary if necessary.  */
476	  if (((const_size + field_size + type_align - 1) / type_align
477	       - const_size / type_align)
478	      > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
479	    const_size = CEIL (const_size, type_align) * type_align;
480	}
481#endif
482
483/* No existing machine description uses this parameter.
484   So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS.  */
485#ifdef BITFIELD_NBYTES_LIMITED
486      if (BITFIELD_NBYTES_LIMITED
487	  && TREE_CODE (field) == FIELD_DECL
488	  && TREE_TYPE (field) != error_mark_node
489	  && DECL_BIT_FIELD_TYPE (field)
490	  && !DECL_PACKED (field)
491	  && !integer_zerop (DECL_SIZE (field)))
492	{
493	  int type_align = TYPE_ALIGN (TREE_TYPE (field));
494	  register tree dsize = DECL_SIZE (field);
495	  int field_size = TREE_INT_CST_LOW (dsize);
496
497	  if (maximum_field_alignment != 0)
498	    type_align = MIN (type_align, maximum_field_alignment);
499	  /* ??? This test is opposite the test in the containing if
500	     statement, so this code is unreachable currently.  */
501	  else if (DECL_PACKED (field))
502	    type_align = MIN (type_align, BITS_PER_UNIT);
503
504	  /* A bit field may not span the unit of alignment of its type.
505	     Advance to next boundary if necessary.  */
506	  /* ??? This code should match the code above for the
507	     PCC_BITFIELD_TYPE_MATTERS case.  */
508	  if (const_size / type_align
509	      != (const_size + field_size - 1) / type_align)
510	    const_size = CEIL (const_size, type_align) * type_align;
511	}
512#endif
513
514      /* Size so far becomes the position of this field.  */
515
516      if (var_size && const_size)
517	DECL_FIELD_BITPOS (field)
518	  = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
519      else if (var_size)
520	DECL_FIELD_BITPOS (field) = var_size;
521      else
522	{
523	  DECL_FIELD_BITPOS (field) = size_int (const_size);
524
525	  /* If this field ended up more aligned than we thought it
526	     would be (we approximate this by seeing if its position
527	     changed), lay out the field again; perhaps we can use an
528	     integral mode for it now.  */
529	  if (known_align != const_size)
530	    layout_decl (field, const_size);
531	}
532
533      /* Now add size of this field to the size of the record.  */
534
535      {
536        register tree dsize = DECL_SIZE (field);
537
538	/* This can happen when we have an invalid nested struct definition,
539	   such as struct j { struct j { int i; } }.  The error message is
540	   printed in finish_struct.  */
541	if (dsize == 0)
542	  /* Do nothing.  */;
543	else if (TREE_CODE (dsize) == INTEGER_CST
544		 && ! TREE_CONSTANT_OVERFLOW (dsize)
545		 && TREE_INT_CST_HIGH (dsize) == 0
546		 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
547	  /* Use const_size if there's no overflow.  */
548	  const_size += TREE_INT_CST_LOW (dsize);
549	else
550	  {
551	    if (var_size == 0)
552	      var_size = dsize;
553	    else
554	      var_size = size_binop (PLUS_EXPR, var_size, dsize);
555	  }
556      }
557    }
558
559  /* Work out the total size and alignment of the record
560     as one expression and store in the record type.
561     Round it up to a multiple of the record's alignment.  */
562
563  if (var_size == 0)
564    {
565      TYPE_SIZE (rec) = size_int (const_size);
566    }
567  else
568    {
569      if (const_size)
570	var_size
571	  = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
572      TYPE_SIZE (rec) = var_size;
573    }
574
575  /* Determine the desired alignment.  */
576#ifdef ROUND_TYPE_ALIGN
577  TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
578#else
579  TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
580#endif
581
582  /* Record the un-rounded size in the binfo node.  But first we check
583     the size of TYPE_BINFO to make sure that BINFO_SIZE is available.  */
584  if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
585    TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
586
587#ifdef ROUND_TYPE_SIZE
588  TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
589#else
590  /* Round the size up to be a multiple of the required alignment */
591  TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
592#endif
593
594  return pending_statics;
595}
596
597/* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
598   Lay out all the fields, set their positions to zero,
599   and compute the size and alignment of the union (maximum of any field).
600   Note that if you set the TYPE_ALIGN before calling this
601   then the union align is aligned to at least that boundary.  */
602
603static void
604layout_union (rec)
605     tree rec;
606{
607  register tree field;
608  unsigned union_align = BITS_PER_UNIT;
609
610  /* The size of the union, based on the fields scanned so far,
611     is max (CONST_SIZE, VAR_SIZE).
612     VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
613  register int const_size = 0;
614  register tree var_size = 0;
615
616#ifdef STRUCTURE_SIZE_BOUNDARY
617  /* Packed structures don't need to have minimum size.  */
618  if (! TYPE_PACKED (rec))
619    union_align = STRUCTURE_SIZE_BOUNDARY;
620#endif
621
622  /* If this is a QUAL_UNION_TYPE, we want to process the fields in
623     the reverse order in building the COND_EXPR that denotes its
624     size.  We reverse them again later.  */
625  if (TREE_CODE (rec) == QUAL_UNION_TYPE)
626    TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
627
628  for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
629    {
630      /* Enums which are local to this class need not be laid out.  */
631      if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
632	continue;
633
634      layout_decl (field, 0);
635      DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
636
637      /* Union must be at least as aligned as any field requires.  */
638
639      union_align = MAX (union_align, DECL_ALIGN (field));
640
641#ifdef PCC_BITFIELD_TYPE_MATTERS
642      /* On the m88000, a bit field of declare type `int'
643	 forces the entire union to have `int' alignment.  */
644      if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
645	union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
646#endif
647
648      if (TREE_CODE (rec) == UNION_TYPE)
649	{
650	  /* Set union_size to max (decl_size, union_size).
651	     There are more and less general ways to do this.
652	     Use only CONST_SIZE unless forced to use VAR_SIZE.  */
653
654	  if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
655	    const_size
656	      = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
657	  else if (var_size == 0)
658	    var_size = DECL_SIZE (field);
659	  else
660	    var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
661	}
662      else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
663	var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
664				DECL_SIZE (field),
665				var_size ? var_size : bitsize_int (0L, 0L)));
666      }
667
668  if (TREE_CODE (rec) == QUAL_UNION_TYPE)
669    TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
670
671  /* Determine the ultimate size of the union (in bytes).  */
672  if (NULL == var_size)
673    TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
674				   * BITS_PER_UNIT, 0L);
675  else if (const_size == 0)
676    TYPE_SIZE (rec) = var_size;
677  else
678    TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
679				  round_up (bitsize_int (const_size, 0L),
680					    BITS_PER_UNIT));
681
682  /* Determine the desired alignment.  */
683#ifdef ROUND_TYPE_ALIGN
684  TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
685#else
686  TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
687#endif
688
689#ifdef ROUND_TYPE_SIZE
690  TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
691#else
692  /* Round the size up to be a multiple of the required alignment */
693  TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
694#endif
695}
696
697/* Calculate the mode, size, and alignment for TYPE.
698   For an array type, calculate the element separation as well.
699   Record TYPE on the chain of permanent or temporary types
700   so that dbxout will find out about it.
701
702   TYPE_SIZE of a type is nonzero if the type has been laid out already.
703   layout_type does nothing on such a type.
704
705   If the type is incomplete, its TYPE_SIZE remains zero.  */
706
707void
708layout_type (type)
709     tree type;
710{
711  int old;
712  tree pending_statics;
713
714  if (type == 0)
715    abort ();
716
717  /* Do nothing if type has been laid out before.  */
718  if (TYPE_SIZE (type))
719    return;
720
721  /* Make sure all nodes we allocate are not momentary;
722     they must last past the current statement.  */
723  old = suspend_momentary ();
724
725  /* Put all our nodes into the same obstack as the type.  Also,
726     make expressions saveable (this is a no-op for permanent types).  */
727
728  push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
729  saveable_allocation ();
730
731  switch (TREE_CODE (type))
732    {
733    case LANG_TYPE:
734      /* This kind of type is the responsibility
735	 of the language-specific code.  */
736      abort ();
737
738    case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill. */
739      if (TYPE_PRECISION (type) == 0)
740	TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
741      /* ... fall through ... */
742
743    case INTEGER_TYPE:
744    case ENUMERAL_TYPE:
745    case CHAR_TYPE:
746      if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
747	  && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
748	TREE_UNSIGNED (type) = 1;
749
750      TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
751						 MODE_INT);
752      TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
753      TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
754      break;
755
756    case REAL_TYPE:
757      TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
758      TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
759      TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
760      break;
761
762    case COMPLEX_TYPE:
763      TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
764      TYPE_MODE (type)
765	= mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
766			 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
767			  ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
768			 0);
769      TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
770      TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
771      break;
772
773    case VOID_TYPE:
774      TYPE_SIZE (type) = size_zero_node;
775      TYPE_SIZE_UNIT (type) = size_zero_node;
776      TYPE_ALIGN (type) = 1;
777      TYPE_MODE (type) = VOIDmode;
778      break;
779
780    case OFFSET_TYPE:
781      TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
782      TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
783      TYPE_MODE (type) = ptr_mode;
784      break;
785
786    case FUNCTION_TYPE:
787    case METHOD_TYPE:
788      TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
789      TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0);
790      TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
791      break;
792
793    case POINTER_TYPE:
794    case REFERENCE_TYPE:
795      TYPE_MODE (type) = ptr_mode;
796      TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
797      TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
798      TREE_UNSIGNED (type) = 1;
799      TYPE_PRECISION (type) = POINTER_SIZE;
800      break;
801
802    case ARRAY_TYPE:
803      {
804	register tree index = TYPE_DOMAIN (type);
805	register tree element = TREE_TYPE (type);
806
807	build_pointer_type (element);
808
809	/* We need to know both bounds in order to compute the size.  */
810	if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
811	    && TYPE_SIZE (element))
812	  {
813	    tree ub = TYPE_MAX_VALUE (index);
814	    tree lb = TYPE_MIN_VALUE (index);
815	    tree length;
816	    tree element_size;
817
818	    /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
819	       test for negative below covers it.  */
820	    if (TREE_CODE (ub) == MAX_EXPR
821		&& TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
822		&& integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
823		&& operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
824				    lb, 0))
825	      ub = TREE_OPERAND (ub, 1);
826	    else if (TREE_CODE (ub) == MAX_EXPR
827		     && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
828		     && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
829		     && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
830						       0),
831					 lb, 0))
832	      ub = TREE_OPERAND (ub, 0);
833
834	    /* The initial subtraction should happen in the original type so
835	       that (possible) negative values are handled appropriately.  */
836	    length = size_binop (PLUS_EXPR, size_one_node,
837				 fold (build (MINUS_EXPR, TREE_TYPE (lb),
838					      ub, lb)));
839
840	    /* If neither bound is a constant and sizetype is signed, make
841	       sure the size is never negative.  We should really do this
842	       if *either* bound is non-constant, but this is the best
843	       compromise between C and Ada.  */
844	    if (! TREE_UNSIGNED (sizetype)
845		&& TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
846		&& TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
847	      length = size_binop (MAX_EXPR, length, size_zero_node);
848
849	    /* Special handling for arrays of bits (for Chill).  */
850	    element_size = TYPE_SIZE (element);
851	    if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
852	      {
853		HOST_WIDE_INT maxvalue, minvalue;
854		maxvalue = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
855		minvalue = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
856		if (maxvalue - minvalue == 1
857		    && (maxvalue == 1 || maxvalue == 0))
858		  element_size = integer_one_node;
859	      }
860
861	    TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length);
862
863	    /* If we know the size of the element, calculate the total
864	       size directly, rather than do some division thing below.
865	       This optimization helps Fortran assumed-size arrays
866	       (where the size of the array is determined at runtime)
867	       substantially.
868	       Note that we can't do this in the case where the size of
869	       the elements is one bit since TYPE_SIZE_UNIT cannot be
870	       set correctly in that case.  */
871	    if (TYPE_SIZE_UNIT (element) != 0
872		&& element_size != integer_one_node)
873	      {
874	        TYPE_SIZE_UNIT (type)
875		  = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
876	      }
877	  }
878
879	/* Now round the alignment and size,
880	   using machine-dependent criteria if any.  */
881
882#ifdef ROUND_TYPE_ALIGN
883	TYPE_ALIGN (type)
884	  = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
885#else
886	TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
887#endif
888
889#ifdef ROUND_TYPE_SIZE
890	if (TYPE_SIZE (type) != 0)
891	  {
892	    tree tmp;
893	    tmp = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
894	    /* If the rounding changed the size of the type, remove any
895	       pre-calculated TYPE_SIZE_UNIT.  */
896	    if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
897	      TYPE_SIZE_UNIT (type) = NULL;
898	    TYPE_SIZE (type) = tmp;
899	  }
900#endif
901
902	TYPE_MODE (type) = BLKmode;
903	if (TYPE_SIZE (type) != 0
904	    && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
905	    /* BLKmode elements force BLKmode aggregate;
906	       else extract/store fields may lose.  */
907	    && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
908		|| TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
909	  {
910	    TYPE_MODE (type)
911	      = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
912			       MODE_INT, 1);
913
914	    if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
915		&& (int)TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
916		&& TYPE_MODE (type) != BLKmode)
917	      {
918		TYPE_NO_FORCE_BLK (type) = 1;
919		TYPE_MODE (type) = BLKmode;
920	      }
921	  }
922	break;
923      }
924
925    case RECORD_TYPE:
926      pending_statics = layout_record (type);
927      TYPE_MODE (type) = BLKmode;
928      if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
929	{
930	  tree field;
931	  enum machine_mode mode = VOIDmode;
932
933	  /* A record which has any BLKmode members must itself be BLKmode;
934	     it can't go in a register.
935	     Unless the member is BLKmode only because it isn't aligned.  */
936	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
937	    {
938	      int bitpos;
939
940	      if (TREE_CODE (field) != FIELD_DECL)
941		continue;
942
943	      if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
944		  && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
945		goto record_lose;
946
947	      if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
948		goto record_lose;
949
950	      bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
951
952	      /* Must be BLKmode if any field crosses a word boundary,
953		 since extract_bit_field can't handle that in registers.  */
954	      if (bitpos / BITS_PER_WORD
955		  != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
956		      / BITS_PER_WORD)
957		  /* But there is no problem if the field is entire words.  */
958		  && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
959		goto record_lose;
960
961	      /* If this field is the whole struct, remember its mode so
962		 that, say, we can put a double in a class into a DF
963		 register instead of forcing it to live in the stack.  */
964	      if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
965		mode = DECL_MODE (field);
966	    }
967
968	  if (mode != VOIDmode)
969	    /* We only have one real field; use its mode.  */
970	    TYPE_MODE (type) = mode;
971	  else
972	    TYPE_MODE (type)
973	      = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
974			       MODE_INT, 1);
975
976	  /* If structure's known alignment is less than
977	     what the scalar mode would need, and it matters,
978	     then stick with BLKmode.  */
979	  if (STRICT_ALIGNMENT
980	      && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
981		    || ((int)TYPE_ALIGN (type)
982			>= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
983	    {
984	      if (TYPE_MODE (type) != BLKmode)
985		/* If this is the only reason this type is BLKmode,
986		   then don't force containing types to be BLKmode.  */
987		TYPE_NO_FORCE_BLK (type) = 1;
988	      TYPE_MODE (type) = BLKmode;
989	    }
990
991	record_lose: ;
992	}
993
994      /* Lay out any static members.  This is done now
995	 because their type may use the record's type.  */
996      while (pending_statics)
997	{
998	  layout_decl (TREE_VALUE (pending_statics), 0);
999	  pending_statics = TREE_CHAIN (pending_statics);
1000	}
1001      break;
1002
1003    case UNION_TYPE:
1004    case QUAL_UNION_TYPE:
1005      layout_union (type);
1006      TYPE_MODE (type) = BLKmode;
1007      if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1008	  /* If structure's known alignment is less than
1009	     what the scalar mode would need, and it matters,
1010	     then stick with BLKmode.  */
1011	  && (! STRICT_ALIGNMENT
1012	      || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1013	      || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1014	{
1015	  tree field;
1016	  /* A union which has any BLKmode members must itself be BLKmode;
1017	     it can't go in a register.
1018	     Unless the member is BLKmode only because it isn't aligned.  */
1019	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1020	    {
1021	      if (TREE_CODE (field) != FIELD_DECL)
1022		continue;
1023
1024	      if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1025		  && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1026		goto union_lose;
1027	    }
1028
1029	  TYPE_MODE (type)
1030	    = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1031			     MODE_INT, 1);
1032
1033	union_lose: ;
1034	}
1035      break;
1036
1037    case SET_TYPE:  /* Used by Chill and Pascal. */
1038      if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1039	  || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1040	abort();
1041      else
1042	{
1043#ifndef SET_WORD_SIZE
1044#define SET_WORD_SIZE BITS_PER_WORD
1045#endif
1046	  int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1047	  int size_in_bits
1048	    = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1049	       - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1050	  int rounded_size
1051	    = ((size_in_bits + alignment - 1) / alignment) * alignment;
1052	  if (rounded_size > alignment)
1053	    TYPE_MODE (type) = BLKmode;
1054	  else
1055	    TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1056	  TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1057	  TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1058	  TYPE_ALIGN (type) = alignment;
1059	  TYPE_PRECISION (type) = size_in_bits;
1060	}
1061      break;
1062
1063    case FILE_TYPE:
1064      /* The size may vary in different languages, so the language front end
1065	 should fill in the size.  */
1066      TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1067      TYPE_MODE  (type) = BLKmode;
1068      break;
1069
1070    default:
1071      abort ();
1072    } /* end switch */
1073
1074  /* Normally, use the alignment corresponding to the mode chosen.
1075     However, where strict alignment is not required, avoid
1076     over-aligning structures, since most compilers do not do this
1077     alignment.  */
1078
1079  if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1080      && (STRICT_ALIGNMENT
1081	  || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1082	      && TREE_CODE (type) != QUAL_UNION_TYPE
1083	      && TREE_CODE (type) != ARRAY_TYPE)))
1084    TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1085
1086  /* Do machine-dependent extra alignment.  */
1087#ifdef ROUND_TYPE_ALIGN
1088  TYPE_ALIGN (type)
1089    = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1090#endif
1091
1092#ifdef ROUND_TYPE_SIZE
1093  if (TYPE_SIZE (type) != 0)
1094    TYPE_SIZE (type)
1095      = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1096#endif
1097
1098  /* Evaluate nonconstant size only once, either now or as soon as safe.  */
1099  if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1100    TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1101
1102  /* If we failed to find a simple way to calculate the unit size
1103     of the type above, find it by division.  */
1104  if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1105    {
1106      TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1107				          size_int (BITS_PER_UNIT));
1108    }
1109
1110  /* Once again evaluate only once, either now or as soon as safe.  */
1111  if (TYPE_SIZE_UNIT (type) != 0
1112      && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1113    TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1114
1115  /* Also layout any other variants of the type.  */
1116  if (TYPE_NEXT_VARIANT (type)
1117      || type != TYPE_MAIN_VARIANT (type))
1118    {
1119      tree variant;
1120      /* Record layout info of this variant.  */
1121      tree size = TYPE_SIZE (type);
1122      tree size_unit = TYPE_SIZE_UNIT (type);
1123      int align = TYPE_ALIGN (type);
1124      enum machine_mode mode = TYPE_MODE (type);
1125
1126      /* Copy it into all variants.  */
1127      for (variant = TYPE_MAIN_VARIANT (type);
1128	   variant;
1129	   variant = TYPE_NEXT_VARIANT (variant))
1130	{
1131	  TYPE_SIZE (variant) = size;
1132	  TYPE_SIZE_UNIT (variant) = size_unit;
1133	  TYPE_ALIGN (variant) = align;
1134	  TYPE_MODE (variant) = mode;
1135	}
1136    }
1137
1138  pop_obstacks ();
1139  resume_momentary (old);
1140}
1141
1142/* Create and return a type for signed integers of PRECISION bits.  */
1143
1144tree
1145make_signed_type (precision)
1146     int precision;
1147{
1148  register tree type = make_node (INTEGER_TYPE);
1149
1150  TYPE_PRECISION (type) = precision;
1151
1152  /* Create the extreme values based on the number of bits.  */
1153
1154  TYPE_MIN_VALUE (type)
1155    = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1156		    ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1157		   (((HOST_WIDE_INT) (-1)
1158		     << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1159			 ? precision - HOST_BITS_PER_WIDE_INT - 1
1160			 : 0))));
1161  TYPE_MAX_VALUE (type)
1162    = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1163		    ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1164		   (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1165		    ? (((HOST_WIDE_INT) 1
1166			<< (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1167		    : 0));
1168
1169  /* Give this type's extreme values this type as their type.  */
1170
1171  TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1172  TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1173
1174  /* The first type made with this or `make_unsigned_type'
1175     is the type for size values.  */
1176
1177  if (sizetype == 0)
1178    set_sizetype (type);
1179
1180  /* Lay out the type: set its alignment, size, etc.  */
1181
1182  layout_type (type);
1183
1184  return type;
1185}
1186
1187/* Create and return a type for unsigned integers of PRECISION bits.  */
1188
1189tree
1190make_unsigned_type (precision)
1191     int precision;
1192{
1193  register tree type = make_node (INTEGER_TYPE);
1194
1195  TYPE_PRECISION (type) = precision;
1196
1197  /* The first type made with this or `make_signed_type'
1198     is the type for size values.  */
1199
1200  if (sizetype == 0)
1201    {
1202      TREE_UNSIGNED (type) = 1;
1203      set_sizetype (type);
1204    }
1205
1206  fixup_unsigned_type (type);
1207  return type;
1208}
1209
1210/* Set sizetype to TYPE, and initialize *sizetype accordingly.
1211   Also update the type of any standard type's sizes made so far.  */
1212
1213void
1214set_sizetype (type)
1215     tree type;
1216{
1217  int oprecision = TYPE_PRECISION (type), precision;
1218
1219  sizetype = type;
1220
1221  /* The *bitsizetype types use a precision that avoids overflows when
1222     calculating signed sizes / offsets in bits.
1223
1224     We are allocating bitsizetype once and change it in place when
1225     we decide later that we want to change it.  This way, we avoid the
1226     hassle of changing all the TYPE_SIZE (TREE_TYPE (sometype))
1227     individually in each front end.  */
1228  if (! bitsizetype)
1229    bitsizetype = make_node (INTEGER_TYPE);
1230  if (TYPE_NAME (sizetype) && ! TYPE_NAME (bitsizetype))
1231    TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1232
1233  precision = oprecision + BITS_PER_UNIT_LOG + 1;
1234  /* However, when cross-compiling from a 32 bit to a 64 bit host,
1235     we are limited to 64 bit precision.  */
1236  if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1237    precision = 2 * HOST_BITS_PER_WIDE_INT;
1238  TYPE_PRECISION (bitsizetype) = precision;
1239  if (TREE_UNSIGNED (type))
1240    fixup_unsigned_type (bitsizetype);
1241  else
1242    fixup_signed_type (bitsizetype);
1243  layout_type (bitsizetype);
1244
1245  if (TREE_UNSIGNED (type))
1246    {
1247      usizetype = sizetype;
1248      ubitsizetype = bitsizetype;
1249      ssizetype = make_signed_type (oprecision);
1250      sbitsizetype = make_signed_type (precision);
1251    }
1252  else
1253    {
1254      ssizetype = sizetype;
1255      sbitsizetype = bitsizetype;
1256      usizetype = make_unsigned_type (oprecision);
1257      ubitsizetype = make_unsigned_type (precision);
1258    }
1259}
1260
1261/* Set the extreme values of TYPE based on its precision in bits,
1262   then lay it out.  Used when make_signed_type won't do
1263   because the tree code is not INTEGER_TYPE.
1264   E.g. for Pascal, when the -fsigned-char option is given.  */
1265
1266void
1267fixup_signed_type (type)
1268     tree type;
1269{
1270  register int precision = TYPE_PRECISION (type);
1271
1272  TYPE_MIN_VALUE (type)
1273    = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1274		    ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1275		   (((HOST_WIDE_INT) (-1)
1276		     << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1277			 ? precision - HOST_BITS_PER_WIDE_INT - 1
1278			 : 0))));
1279  TYPE_MAX_VALUE (type)
1280    = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1281		    ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1282		   (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1283		    ? (((HOST_WIDE_INT) 1
1284			<< (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1285		    : 0));
1286
1287  TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1288  TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1289
1290  /* Lay out the type: set its alignment, size, etc.  */
1291
1292  layout_type (type);
1293}
1294
1295/* Set the extreme values of TYPE based on its precision in bits,
1296   then lay it out.  This is used both in `make_unsigned_type'
1297   and for enumeral types.  */
1298
1299void
1300fixup_unsigned_type (type)
1301     tree type;
1302{
1303  register int precision = TYPE_PRECISION (type);
1304
1305  TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1306  TYPE_MAX_VALUE (type)
1307    = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1308		   ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1309		   precision - HOST_BITS_PER_WIDE_INT > 0
1310		   ? ((unsigned HOST_WIDE_INT) ~0
1311		      >> (HOST_BITS_PER_WIDE_INT
1312			  - (precision - HOST_BITS_PER_WIDE_INT)))
1313		   : 0);
1314  TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1315  TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1316
1317  /* Lay out the type: set its alignment, size, etc.  */
1318
1319  layout_type (type);
1320}
1321
1322/* Find the best machine mode to use when referencing a bit field of length
1323   BITSIZE bits starting at BITPOS.
1324
1325   The underlying object is known to be aligned to a boundary of ALIGN bits.
1326   If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1327   larger than LARGEST_MODE (usually SImode).
1328
1329   If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
1330   VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1331   mode meeting these conditions.
1332
1333   Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1334   the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1335   all the conditions.  */
1336
1337enum machine_mode
1338get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1339     int bitsize, bitpos;
1340     int align;
1341     enum machine_mode largest_mode;
1342     int volatilep;
1343{
1344  enum machine_mode mode;
1345  int unit = 0;
1346
1347  /* Find the narrowest integer mode that contains the bit field.  */
1348  for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1349       mode = GET_MODE_WIDER_MODE (mode))
1350    {
1351      unit = GET_MODE_BITSIZE (mode);
1352      if ((bitpos % unit) + bitsize <= unit)
1353	break;
1354    }
1355
1356  if (mode == MAX_MACHINE_MODE
1357      /* It is tempting to omit the following line
1358	 if STRICT_ALIGNMENT is true.
1359	 But that is incorrect, since if the bitfield uses part of 3 bytes
1360	 and we use a 4-byte mode, we could get a spurious segv
1361	 if the extra 4th byte is past the end of memory.
1362	 (Though at least one Unix compiler ignores this problem:
1363	 that on the Sequent 386 machine.  */
1364      || MIN (unit, BIGGEST_ALIGNMENT) > align
1365      || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1366    return VOIDmode;
1367
1368  if (SLOW_BYTE_ACCESS && ! volatilep)
1369    {
1370      enum machine_mode wide_mode = VOIDmode, tmode;
1371
1372      for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1373	   tmode = GET_MODE_WIDER_MODE (tmode))
1374	{
1375	  unit = GET_MODE_BITSIZE (tmode);
1376	  if (bitpos / unit == (bitpos + bitsize - 1) / unit
1377	      && unit <= BITS_PER_WORD
1378	      && unit <= MIN (align, BIGGEST_ALIGNMENT)
1379	      && (largest_mode == VOIDmode
1380		  || unit <= GET_MODE_BITSIZE (largest_mode)))
1381	    wide_mode = tmode;
1382	}
1383
1384      if (wide_mode != VOIDmode)
1385	return wide_mode;
1386    }
1387
1388  return mode;
1389}
1390
1391/* Save all variables describing the current status into the structure *P.
1392   This is used before starting a nested function.  */
1393
1394void
1395save_storage_status (p)
1396     struct function *p ATTRIBUTE_UNUSED;
1397{
1398#if 0  /* Need not save, since always 0 and non0 (resp.) within a function.  */
1399  p->pending_sizes = pending_sizes;
1400  p->immediate_size_expand = immediate_size_expand;
1401#endif /* 0 */
1402}
1403
1404/* Restore all variables describing the current status from the structure *P.
1405   This is used after a nested function.  */
1406
1407void
1408restore_storage_status (p)
1409     struct function *p ATTRIBUTE_UNUSED;
1410{
1411#if 0
1412  pending_sizes = p->pending_sizes;
1413  immediate_size_expand = p->immediate_size_expand;
1414#endif /* 0 */
1415}
1416