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