]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/stor-layout.c
Remove obstacks.
[thirdparty/gcc.git] / gcc / stor-layout.c
CommitLineData
7306ed3f 1/* C-compiler utilities for types and variables storage layout
06ceef4e
RK
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
7306ed3f
JW
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
7306ed3f
JW
21
22
23#include "config.h"
670ee920 24#include "system.h"
7306ed3f 25#include "tree.h"
d05a5492 26#include "rtl.h"
6baf1cc8 27#include "tm_p.h"
566cdc73 28#include "flags.h"
7306ed3f 29#include "function.h"
234042f4 30#include "expr.h"
10f0ad3d 31#include "toplev.h"
d7db6646 32#include "ggc.h"
7306ed3f 33
fed3cef0
RK
34/* Set to one when set_sizetype has been called. */
35static int sizetype_set;
36
37/* List of types created before set_sizetype has been called. We do not
38 make this a GGC root since we want these nodes to be reclaimed. */
39static tree early_type_list;
40
7306ed3f 41/* Data type for the expressions representing sizes of data types.
896cced4 42 It is the first integer type laid out. */
fed3cef0 43tree sizetype_tab[(int) TYPE_KIND_LAST];
7306ed3f 44
d4c40650
RS
45/* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
729a2125 47unsigned int maximum_field_alignment;
d4c40650 48
abc95ed3 49/* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
b5d11e41 50 May be overridden by front-ends. */
729a2125 51unsigned int set_alignment = 0;
b5d11e41 52
770ae6cc 53static void finalize_record_size PARAMS ((record_layout_info));
770ae6cc
RK
54static void finalize_type_size PARAMS ((tree));
55static void place_union_field PARAMS ((record_layout_info, tree));
36244024 56extern void debug_rli PARAMS ((record_layout_info));
7306ed3f
JW
57\f
58/* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
59
60static tree pending_sizes;
61
62/* Nonzero means cannot safely call expand_expr now,
63 so put variable sizes onto `pending_sizes' instead. */
64
65int immediate_size_expand;
66
770ae6cc
RK
67/* Get a list of all the objects put on the pending sizes list. */
68
7306ed3f
JW
69tree
70get_pending_sizes ()
71{
72 tree chain = pending_sizes;
4e4b555d
RS
73 tree t;
74
75 /* Put each SAVE_EXPR into the current function. */
76 for (t = chain; t; t = TREE_CHAIN (t))
77 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
d4b60170 78
7306ed3f
JW
79 pending_sizes = 0;
80 return chain;
81}
82
770ae6cc
RK
83/* Put a chain of objects into the pending sizes list, which must be
84 empty. */
85
1fd7c4ac
RK
86void
87put_pending_sizes (chain)
88 tree chain;
89{
90 if (pending_sizes)
91 abort ();
92
93 pending_sizes = chain;
94}
95
76ffb3a0 96/* Given a size SIZE that may not be a constant, return a SAVE_EXPR
7306ed3f
JW
97 to serve as the actual size-expression for a type or decl. */
98
4e4b555d 99tree
7306ed3f
JW
100variable_size (size)
101 tree size;
102{
5e9bec99
RK
103 /* If the language-processor is to take responsibility for variable-sized
104 items (e.g., languages which have elaboration procedures like Ada),
e5852cff 105 just return SIZE unchanged. Likewise for self-referential sizes. */
76ffb3a0
RK
106 if (TREE_CONSTANT (size)
107 || global_bindings_p () < 0 || contains_placeholder_p (size))
5e9bec99
RK
108 return size;
109
68de3831
RK
110 size = save_expr (size);
111
d26f8097
MM
112 /* If an array with a variable number of elements is declared, and
113 the elements require destruction, we will emit a cleanup for the
114 array. That cleanup is run both on normal exit from the block
115 and in the exception-handler for the block. Normally, when code
116 is used in both ordinary code and in an exception handler it is
117 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
118 not wish to do that here; the array-size is the same in both
119 places. */
120 if (TREE_CODE (size) == SAVE_EXPR)
121 SAVE_EXPR_PERSISTENT_P (size) = 1;
122
68de3831 123 if (global_bindings_p ())
7306ed3f 124 {
80f9c711
RS
125 if (TREE_CONSTANT (size))
126 error ("type size can't be explicitly evaluated");
127 else
128 error ("variable-size type declared outside of any function");
129
fed3cef0 130 return size_one_node;
7306ed3f
JW
131 }
132
133 if (immediate_size_expand)
93609dfb
RK
134 /* NULL_RTX is not defined; neither is the rtx type.
135 Also, we would like to pass const0_rtx here, but don't have it. */
136 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
137 VOIDmode, 0);
770ae6cc 138 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
d43163b7
MM
139 /* The front-end doesn't want us to keep a list of the expressions
140 that determine sizes for variable size objects. */
141 ;
7306ed3f 142 else
c166a311 143 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
7306ed3f
JW
144
145 return size;
146}
147\f
148#ifndef MAX_FIXED_MODE_SIZE
149#define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
150#endif
151
152/* Return the machine mode to use for a nonscalar of SIZE bits.
153 The mode must be in class CLASS, and have exactly that many bits.
154 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
155 be used. */
156
157enum machine_mode
158mode_for_size (size, class, limit)
770ae6cc 159 unsigned int size;
7306ed3f
JW
160 enum mode_class class;
161 int limit;
162{
163 register enum machine_mode mode;
164
72c602fc 165 if (limit && size > MAX_FIXED_MODE_SIZE)
7306ed3f
JW
166 return BLKmode;
167
5e9bec99 168 /* Get the first mode which has this size, in the specified class. */
7306ed3f
JW
169 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
170 mode = GET_MODE_WIDER_MODE (mode))
72c602fc 171 if (GET_MODE_BITSIZE (mode) == size)
7306ed3f
JW
172 return mode;
173
174 return BLKmode;
175}
176
72c602fc
RK
177/* Similar, except passed a tree node. */
178
179enum machine_mode
180mode_for_size_tree (size, class, limit)
181 tree size;
182 enum mode_class class;
183 int limit;
184{
185 if (TREE_CODE (size) != INTEGER_CST
72c602fc
RK
186 /* What we really want to say here is that the size can fit in a
187 host integer, but we know there's no way we'd find a mode for
188 this many bits, so there's no point in doing the precise test. */
05bccae2 189 || compare_tree_int (size, 1000) > 0)
72c602fc
RK
190 return BLKmode;
191 else
192 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
193}
194
5e9bec99
RK
195/* Similar, but never return BLKmode; return the narrowest mode that
196 contains at least the requested number of bits. */
197
27922c13 198enum machine_mode
5e9bec99 199smallest_mode_for_size (size, class)
770ae6cc 200 unsigned int size;
5e9bec99
RK
201 enum mode_class class;
202{
203 register enum machine_mode mode;
204
205 /* Get the first mode which has at least this size, in the
206 specified class. */
207 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
208 mode = GET_MODE_WIDER_MODE (mode))
72c602fc 209 if (GET_MODE_BITSIZE (mode) >= size)
5e9bec99
RK
210 return mode;
211
212 abort ();
213}
214
d006aa54
RH
215/* Find an integer mode of the exact same size, or BLKmode on failure. */
216
217enum machine_mode
218int_mode_for_mode (mode)
219 enum machine_mode mode;
220{
221 switch (GET_MODE_CLASS (mode))
222 {
223 case MODE_INT:
224 case MODE_PARTIAL_INT:
225 break;
226
227 case MODE_COMPLEX_INT:
228 case MODE_COMPLEX_FLOAT:
229 case MODE_FLOAT:
230 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
231 break;
232
233 case MODE_RANDOM:
234 if (mode == BLKmode)
235 break;
d4b60170
RK
236
237 /* ... fall through ... */
d006aa54
RH
238
239 case MODE_CC:
240 default:
05bccae2 241 abort ();
d006aa54
RH
242 }
243
244 return mode;
245}
246
fed3cef0
RK
247/* Return the value of VALUE, rounded up to a multiple of DIVISOR.
248 This can only be applied to objects of a sizetype. */
7306ed3f
JW
249
250tree
251round_up (value, divisor)
252 tree value;
253 int divisor;
254{
fed3cef0
RK
255 tree arg = size_int_type (divisor, TREE_TYPE (value));
256
257 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
258}
259
260/* Likewise, but round down. */
261
262tree
263round_down (value, divisor)
264 tree value;
265 int divisor;
266{
267 tree arg = size_int_type (divisor, TREE_TYPE (value));
268
269 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
7306ed3f
JW
270}
271\f
272/* Set the size, mode and alignment of a ..._DECL node.
273 TYPE_DECL does need this for C++.
274 Note that LABEL_DECL and CONST_DECL nodes do not need this,
275 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
276 Don't call layout_decl for them.
277
278 KNOWN_ALIGN is the amount of alignment we can assume this
279 decl has with no special effort. It is relevant only for FIELD_DECLs
280 and depends on the previous fields.
281 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
282 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
283 the record will be aligned to suit. */
284
285void
286layout_decl (decl, known_align)
287 tree decl;
729a2125 288 unsigned int known_align;
7306ed3f
JW
289{
290 register tree type = TREE_TYPE (decl);
291 register enum tree_code code = TREE_CODE (decl);
7306ed3f
JW
292
293 if (code == CONST_DECL)
294 return;
9df2c88c 295 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
33433751 296 && code != TYPE_DECL && code != FIELD_DECL)
7306ed3f
JW
297 abort ();
298
299 if (type == error_mark_node)
33433751 300 type = void_type_node;
7306ed3f 301
770ae6cc
RK
302 /* Usually the size and mode come from the data type without change,
303 however, the front-end may set the explicit width of the field, so its
304 size may not be the same as the size of its type. This happens with
305 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
306 also happens with other fields. For example, the C++ front-end creates
307 zero-sized fields corresponding to empty base classes, and depends on
308 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
4b6bf620
RK
309 size in bytes from the size in bits. If we have already set the mode,
310 don't set it again since we can be called twice for FIELD_DECLs. */
770ae6cc 311
7306ed3f 312 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
4b6bf620
RK
313 if (DECL_MODE (decl) == VOIDmode)
314 DECL_MODE (decl) = TYPE_MODE (type);
770ae6cc 315
5e9bec99 316 if (DECL_SIZE (decl) == 0)
06ceef4e
RK
317 {
318 DECL_SIZE (decl) = TYPE_SIZE (type);
319 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
320 }
770ae6cc
RK
321 else
322 DECL_SIZE_UNIT (decl)
323 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
324 bitsize_unit_node));
06ceef4e 325
7306ed3f 326 /* Force alignment required for the data type.
23ad4d41
RS
327 But if the decl itself wants greater alignment, don't override that.
328 Likewise, if the decl is packed, don't override it. */
17aec3eb 329 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
33433751 330 && (DECL_ALIGN (decl) == 0
17aec3eb
RK
331 || (! (code == FIELD_DECL && DECL_PACKED (decl))
332 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
11cf4d18
JJ
333 {
334 DECL_ALIGN (decl) = TYPE_ALIGN (type);
335 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
336 }
7306ed3f 337
770ae6cc 338 /* For fields, set the bit field type and update the alignment. */
7306ed3f 339 if (code == FIELD_DECL)
d4c40650
RS
340 {
341 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
342 if (maximum_field_alignment != 0)
729a2125 343 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
3a369acd 344 else if (DECL_PACKED (decl))
11cf4d18
JJ
345 {
346 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
347 DECL_USER_ALIGN (decl) = 0;
348 }
d4c40650
RS
349 }
350
770ae6cc
RK
351 /* See if we can use an ordinary integer mode for a bit-field.
352 Conditions are: a fixed size that is correct for another mode
353 and occupying a complete byte or bytes on proper boundary. */
17aec3eb 354 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
7306ed3f 355 && TYPE_SIZE (type) != 0
5bb3d1dd
RK
356 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
357 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
7306ed3f
JW
358 {
359 register enum machine_mode xmode
72c602fc 360 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
7306ed3f 361
b1ba4cc3 362 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
7306ed3f 363 {
729a2125 364 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
7306ed3f
JW
365 DECL_ALIGN (decl));
366 DECL_MODE (decl) = xmode;
7306ed3f
JW
367 DECL_BIT_FIELD (decl) = 0;
368 }
369 }
370
977a7752 371 /* Turn off DECL_BIT_FIELD if we won't need it set. */
17aec3eb 372 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
770ae6cc 373 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
b1ba4cc3 374 && known_align >= TYPE_ALIGN (type)
770ae6cc
RK
375 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
376 && DECL_SIZE_UNIT (decl) != 0)
977a7752
RK
377 DECL_BIT_FIELD (decl) = 0;
378
7306ed3f
JW
379 /* Evaluate nonconstant size only once, either now or as soon as safe. */
380 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
381 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
06ceef4e
RK
382 if (DECL_SIZE_UNIT (decl) != 0
383 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
384 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
385
386 /* If requested, warn about definitions of large data objects. */
387 if (warn_larger_than
17aec3eb 388 && (code == VAR_DECL || code == PARM_DECL)
06ceef4e
RK
389 && ! DECL_EXTERNAL (decl))
390 {
391 tree size = DECL_SIZE_UNIT (decl);
392
393 if (size != 0 && TREE_CODE (size) == INTEGER_CST
05bccae2 394 && compare_tree_int (size, larger_than_size) > 0)
06ceef4e 395 {
05bccae2 396 unsigned int size_as_int = TREE_INT_CST_LOW (size);
06ceef4e 397
05bccae2 398 if (compare_tree_int (size, size_as_int) == 0)
06ceef4e
RK
399 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
400 else
401 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
402 larger_than_size);
403 }
404 }
7306ed3f
JW
405}
406\f
770ae6cc
RK
407/* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
408 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
409 is to be passed to all other layout functions for this record. It is the
410 responsibility of the caller to call `free' for the storage returned.
411 Note that garbage collection is not permitted until we finish laying
412 out the record. */
7306ed3f 413
9328904c 414record_layout_info
770ae6cc 415start_record_layout (t)
9328904c 416 tree t;
7306ed3f 417{
9328904c 418 record_layout_info rli
defd0dea 419 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
9328904c
MM
420
421 rli->t = t;
770ae6cc 422
9328904c
MM
423 /* If the type has a minimum specified alignment (via an attribute
424 declaration, for example) use it -- otherwise, start with a
425 one-byte alignment. */
426 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
427 rli->unpacked_align = rli->record_align;
770ae6cc 428 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
7306ed3f 429
5c19a356
MS
430#ifdef STRUCTURE_SIZE_BOUNDARY
431 /* Packed structures don't need to have minimum size. */
f132af85 432 if (! TYPE_PACKED (t))
9328904c 433 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
5c19a356 434#endif
7306ed3f 435
770ae6cc
RK
436 rli->offset = size_zero_node;
437 rli->bitpos = bitsize_zero_node;
438 rli->pending_statics = 0;
439 rli->packed_maybe_necessary = 0;
440
9328904c
MM
441 return rli;
442}
7306ed3f 443
f2704b9f
RK
444/* These four routines perform computations that convert between
445 the offset/bitpos forms and byte and bit offsets. */
446
447tree
448bit_from_pos (offset, bitpos)
449 tree offset, bitpos;
450{
451 return size_binop (PLUS_EXPR, bitpos,
452 size_binop (MULT_EXPR, convert (bitsizetype, offset),
453 bitsize_unit_node));
454}
455
456tree
457byte_from_pos (offset, bitpos)
458 tree offset, bitpos;
459{
460 return size_binop (PLUS_EXPR, offset,
461 convert (sizetype,
f0fddb15 462 size_binop (TRUNC_DIV_EXPR, bitpos,
f2704b9f
RK
463 bitsize_unit_node)));
464}
465
466void
467pos_from_byte (poffset, pbitpos, off_align, pos)
468 tree *poffset, *pbitpos;
469 unsigned int off_align;
470 tree pos;
471{
472 *poffset
473 = size_binop (MULT_EXPR,
474 convert (sizetype,
475 size_binop (FLOOR_DIV_EXPR, pos,
476 bitsize_int (off_align
477 / BITS_PER_UNIT))),
478 size_int (off_align / BITS_PER_UNIT));
479 *pbitpos = size_binop (MULT_EXPR,
480 size_binop (FLOOR_MOD_EXPR, pos,
481 bitsize_int (off_align / BITS_PER_UNIT)),
482 bitsize_unit_node);
483}
484
485void
486pos_from_bit (poffset, pbitpos, off_align, pos)
487 tree *poffset, *pbitpos;
488 unsigned int off_align;
489 tree pos;
490{
491 *poffset = size_binop (MULT_EXPR,
492 convert (sizetype,
493 size_binop (FLOOR_DIV_EXPR, pos,
494 bitsize_int (off_align))),
495 size_int (off_align / BITS_PER_UNIT));
496 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
497}
498
499/* Given a pointer to bit and byte offsets and an offset alignment,
500 normalize the offsets so they are within the alignment. */
501
502void
503normalize_offset (poffset, pbitpos, off_align)
504 tree *poffset, *pbitpos;
505 unsigned int off_align;
506{
507 /* If the bit position is now larger than it should be, adjust it
508 downwards. */
509 if (compare_tree_int (*pbitpos, off_align) >= 0)
510 {
511 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
512 bitsize_int (off_align));
513
514 *poffset
515 = size_binop (PLUS_EXPR, *poffset,
516 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
517 size_int (off_align / BITS_PER_UNIT)));
518
519 *pbitpos
520 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
521 }
522}
523
770ae6cc 524/* Print debugging information about the information in RLI. */
cc9d4a85 525
770ae6cc
RK
526void
527debug_rli (rli)
cc9d4a85 528 record_layout_info rli;
cc9d4a85 529{
770ae6cc
RK
530 print_node_brief (stderr, "type", rli->t, 0);
531 print_node_brief (stderr, "\noffset", rli->offset, 0);
532 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
cc9d4a85 533
770ae6cc
RK
534 fprintf (stderr, "\nrec_align = %u, unpack_align = %u, off_align = %u\n",
535 rli->record_align, rli->unpacked_align, rli->offset_align);
536 if (rli->packed_maybe_necessary)
537 fprintf (stderr, "packed may be necessary\n");
538
539 if (rli->pending_statics)
540 {
541 fprintf (stderr, "pending statics:\n");
542 debug_tree (rli->pending_statics);
543 }
544}
545
546/* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
547 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
548
549void
550normalize_rli (rli)
551 record_layout_info rli;
552{
f2704b9f 553 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
770ae6cc 554}
cc9d4a85 555
770ae6cc
RK
556/* Returns the size in bytes allocated so far. */
557
558tree
559rli_size_unit_so_far (rli)
560 record_layout_info rli;
561{
f2704b9f 562 return byte_from_pos (rli->offset, rli->bitpos);
770ae6cc
RK
563}
564
565/* Returns the size in bits allocated so far. */
566
567tree
568rli_size_so_far (rli)
569 record_layout_info rli;
570{
f2704b9f 571 return bit_from_pos (rli->offset, rli->bitpos);
770ae6cc
RK
572}
573
574/* Called from place_field to handle unions. */
575
576static void
577place_union_field (rli, field)
578 record_layout_info rli;
579 tree field;
580{
11cf4d18
JJ
581 unsigned int desired_align;
582
cc9d4a85 583 layout_decl (field, 0);
770ae6cc
RK
584
585 DECL_FIELD_OFFSET (field) = size_zero_node;
586 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
2f5c7f45 587 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
cc9d4a85 588
11cf4d18
JJ
589 desired_align = DECL_ALIGN (field);
590
591#ifdef BIGGEST_FIELD_ALIGNMENT
592 /* Some targets (i.e. i386) limit union field alignment
593 to a lower boundary than alignment of variables unless
594 it was overridden by attribute aligned. */
595 if (! DECL_USER_ALIGN (field))
596 desired_align =
597 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
598#endif
599
cc9d4a85 600 /* Union must be at least as aligned as any field requires. */
11cf4d18 601 rli->record_align = MAX (rli->record_align, desired_align);
cc9d4a85
MM
602
603#ifdef PCC_BITFIELD_TYPE_MATTERS
604 /* On the m88000, a bit field of declare type `int' forces the
605 entire union to have `int' alignment. */
606 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
607 rli->record_align = MAX (rli->record_align,
608 TYPE_ALIGN (TREE_TYPE (field)));
609#endif
610
770ae6cc
RK
611 /* We assume the union's size will be a multiple of a byte so we don't
612 bother with BITPOS. */
cc9d4a85 613 if (TREE_CODE (rli->t) == UNION_TYPE)
770ae6cc 614 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
cc9d4a85 615 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
770ae6cc
RK
616 rli->offset = fold (build (COND_EXPR, sizetype,
617 DECL_QUALIFIER (field),
618 DECL_SIZE_UNIT (field), rli->offset));
cc9d4a85
MM
619}
620
9328904c
MM
621/* RLI contains information about the layout of a RECORD_TYPE. FIELD
622 is a FIELD_DECL to be added after those fields already present in
623 T. (FIELD is not actually added to the TYPE_FIELDS list here;
624 callers that desire that behavior must manually perform that step.) */
d4b60170 625
9328904c 626void
770ae6cc 627place_field (rli, field)
9328904c
MM
628 record_layout_info rli;
629 tree field;
630{
631 /* The alignment required for FIELD. */
632 unsigned int desired_align;
633 /* The alignment FIELD would have if we just dropped it into the
634 record as it presently stands. */
635 unsigned int known_align;
770ae6cc 636 unsigned int actual_align;
11cf4d18 637 unsigned int user_align;
9328904c
MM
638 /* The type of this field. */
639 tree type = TREE_TYPE (field);
770ae6cc 640
5748b2cb
RK
641 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
642 return;
643
9328904c
MM
644 /* If FIELD is static, then treat it like a separate variable, not
645 really like a structure field. If it is a FUNCTION_DECL, it's a
646 method. In both cases, all we do is lay out the decl, and we do
647 it *after* the record is laid out. */
648 if (TREE_CODE (field) == VAR_DECL)
649 {
650 rli->pending_statics = tree_cons (NULL_TREE, field,
651 rli->pending_statics);
652 return;
653 }
770ae6cc 654
9328904c
MM
655 /* Enumerators and enum types which are local to this class need not
656 be laid out. Likewise for initialized constant fields. */
657 else if (TREE_CODE (field) != FIELD_DECL)
658 return;
770ae6cc
RK
659
660 /* Unions are laid out very differently than records, so split
661 that code off to another function. */
cc9d4a85
MM
662 else if (TREE_CODE (rli->t) != RECORD_TYPE)
663 {
770ae6cc 664 place_union_field (rli, field);
cc9d4a85
MM
665 return;
666 }
7306ed3f 667
770ae6cc
RK
668 /* Work out the known alignment so far. Note that A & (-A) is the
669 value of the least-significant bit in A that is one. */
4b6bf620 670 if (! integer_zerop (rli->bitpos))
770ae6cc
RK
671 known_align = (tree_low_cst (rli->bitpos, 1)
672 & - tree_low_cst (rli->bitpos, 1));
4b6bf620
RK
673 else if (integer_zerop (rli->offset))
674 known_align = BIGGEST_ALIGNMENT;
770ae6cc
RK
675 else if (host_integerp (rli->offset, 1))
676 known_align = (BITS_PER_UNIT
677 * (tree_low_cst (rli->offset, 1)
678 & - tree_low_cst (rli->offset, 1)));
679 else
680 known_align = rli->offset_align;
9328904c
MM
681
682 /* Lay out the field so we know what alignment it needs. For a
683 packed field, use the alignment as specified, disregarding what
684 the type would want. */
770ae6cc 685 desired_align = DECL_ALIGN (field);
11cf4d18 686 user_align = DECL_USER_ALIGN (field);
9328904c
MM
687 layout_decl (field, known_align);
688 if (! DECL_PACKED (field))
11cf4d18
JJ
689 {
690 desired_align = DECL_ALIGN (field);
691 user_align = DECL_USER_ALIGN (field);
692 }
770ae6cc 693
7306ed3f 694#ifdef BIGGEST_FIELD_ALIGNMENT
11cf4d18
JJ
695 /* Some targets (i.e. i386, VMS) limit struct field alignment
696 to a lower boundary than alignment of variables unless
697 it was overridden by attribute aligned. */
698 if (! user_align)
699 desired_align =
700 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
7306ed3f 701#endif
d42d380a 702#ifdef ADJUST_FIELD_ALIGN
9328904c 703 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
d42d380a 704#endif
7306ed3f 705
9328904c
MM
706 /* Record must have at least as much alignment as any field.
707 Otherwise, the alignment of the field within the record is
708 meaningless. */
3c12fcc2 709#ifdef PCC_BITFIELD_TYPE_MATTERS
9328904c
MM
710 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
711 && DECL_BIT_FIELD_TYPE (field)
712 && ! integer_zerop (TYPE_SIZE (type)))
713 {
714 /* For these machines, a zero-length field does not
715 affect the alignment of the structure as a whole.
716 It does, however, affect the alignment of the next field
717 within the structure. */
718 if (! integer_zerop (DECL_SIZE (field)))
719 rli->record_align = MAX (rli->record_align, desired_align);
720 else if (! DECL_PACKED (field))
721 desired_align = TYPE_ALIGN (type);
770ae6cc 722
9328904c
MM
723 /* A named bit field of declared type `int'
724 forces the entire structure to have `int' alignment. */
725 if (DECL_NAME (field) != 0)
7306ed3f 726 {
9328904c 727 unsigned int type_align = TYPE_ALIGN (type);
729a2125 728
9328904c
MM
729 if (maximum_field_alignment != 0)
730 type_align = MIN (type_align, maximum_field_alignment);
731 else if (DECL_PACKED (field))
732 type_align = MIN (type_align, BITS_PER_UNIT);
e2301a83 733
9328904c 734 rli->record_align = MAX (rli->record_align, type_align);
3c12fcc2 735 if (warn_packed)
9328904c
MM
736 rli->unpacked_align = MAX (rli->unpacked_align,
737 TYPE_ALIGN (type));
3c12fcc2 738 }
9328904c
MM
739 }
740 else
741#endif
742 {
743 rli->record_align = MAX (rli->record_align, desired_align);
770ae6cc 744 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
9328904c 745 }
3c12fcc2 746
9328904c
MM
747 if (warn_packed && DECL_PACKED (field))
748 {
770ae6cc 749 if (known_align > TYPE_ALIGN (type))
3c12fcc2 750 {
9328904c 751 if (TYPE_ALIGN (type) > desired_align)
3c12fcc2 752 {
9328904c
MM
753 if (STRICT_ALIGNMENT)
754 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
755 else
756 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
3c12fcc2 757 }
3c12fcc2 758 }
9328904c
MM
759 else
760 rli->packed_maybe_necessary = 1;
761 }
7306ed3f 762
9328904c
MM
763 /* Does this field automatically have alignment it needs by virtue
764 of the fields that precede it and the record's own alignment? */
770ae6cc 765 if (known_align < desired_align)
9328904c
MM
766 {
767 /* No, we need to skip space before this field.
768 Bump the cumulative size to multiple of field alignment. */
7306ed3f 769
9328904c
MM
770 if (warn_padded)
771 warning_with_decl (field, "padding struct to align `%s'");
3c12fcc2 772
770ae6cc
RK
773 /* If the alignment is still within offset_align, just align
774 the bit position. */
775 if (desired_align < rli->offset_align)
776 rli->bitpos = round_up (rli->bitpos, desired_align);
9328904c
MM
777 else
778 {
770ae6cc
RK
779 /* First adjust OFFSET by the partial bits, then align. */
780 rli->offset
781 = size_binop (PLUS_EXPR, rli->offset,
782 convert (sizetype,
783 size_binop (CEIL_DIV_EXPR, rli->bitpos,
784 bitsize_unit_node)));
785 rli->bitpos = bitsize_zero_node;
786
787 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
7306ed3f 788 }
770ae6cc 789
b1254b72
RK
790 if (! TREE_CONSTANT (rli->offset))
791 rli->offset_align = desired_align;
792
9328904c 793 }
7306ed3f 794
770ae6cc
RK
795 /* Handle compatibility with PCC. Note that if the record has any
796 variable-sized fields, we need not worry about compatibility. */
7306ed3f 797#ifdef PCC_BITFIELD_TYPE_MATTERS
9328904c
MM
798 if (PCC_BITFIELD_TYPE_MATTERS
799 && TREE_CODE (field) == FIELD_DECL
800 && type != error_mark_node
770ae6cc
RK
801 && DECL_BIT_FIELD (field)
802 && ! DECL_PACKED (field)
9328904c 803 && maximum_field_alignment == 0
770ae6cc
RK
804 && ! integer_zerop (DECL_SIZE (field))
805 && host_integerp (DECL_SIZE (field), 1)
806 && host_integerp (rli->offset, 1)
807 && host_integerp (TYPE_SIZE (type), 1))
9328904c
MM
808 {
809 unsigned int type_align = TYPE_ALIGN (type);
770ae6cc
RK
810 tree dsize = DECL_SIZE (field);
811 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
812 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
813 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
9328904c
MM
814
815 /* A bit field may not span more units of alignment of its type
816 than its type itself. Advance to next boundary if necessary. */
770ae6cc
RK
817 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
818 type_align - 1)
819 / type_align)
820 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
821 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
822 rli->bitpos = round_up (rli->bitpos, type_align);
9328904c 823 }
7306ed3f
JW
824#endif
825
7306ed3f 826#ifdef BITFIELD_NBYTES_LIMITED
9328904c
MM
827 if (BITFIELD_NBYTES_LIMITED
828 && TREE_CODE (field) == FIELD_DECL
829 && type != error_mark_node
830 && DECL_BIT_FIELD_TYPE (field)
770ae6cc
RK
831 && ! DECL_PACKED (field)
832 && ! integer_zerop (DECL_SIZE (field))
833 && host_integerp (DECL_SIZE (field), 1)
163d3408 834 && host_integerp (rli->offset, 1)
770ae6cc 835 && host_integerp (TYPE_SIZE (type), 1))
9328904c
MM
836 {
837 unsigned int type_align = TYPE_ALIGN (type);
770ae6cc
RK
838 tree dsize = DECL_SIZE (field);
839 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
840 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
841 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
e2301a83 842
9328904c
MM
843 if (maximum_field_alignment != 0)
844 type_align = MIN (type_align, maximum_field_alignment);
845 /* ??? This test is opposite the test in the containing if
846 statement, so this code is unreachable currently. */
847 else if (DECL_PACKED (field))
848 type_align = MIN (type_align, BITS_PER_UNIT);
849
850 /* A bit field may not span the unit of alignment of its type.
851 Advance to next boundary if necessary. */
852 /* ??? This code should match the code above for the
853 PCC_BITFIELD_TYPE_MATTERS case. */
770ae6cc
RK
854 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
855 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
856 / type_align))
857 rli->bitpos = round_up (rli->bitpos, type_align);
9328904c 858 }
7306ed3f
JW
859#endif
860
770ae6cc
RK
861 /* Offset so far becomes the position of this field after normalizing. */
862 normalize_rli (rli);
863 DECL_FIELD_OFFSET (field) = rli->offset;
864 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
2f5c7f45 865 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
770ae6cc
RK
866
867 /* If this field ended up more aligned than we thought it would be (we
868 approximate this by seeing if its position changed), lay out the field
869 again; perhaps we can use an integral mode for it now. */
4b6bf620 870 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
770ae6cc
RK
871 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
872 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
4b6bf620
RK
873 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
874 actual_align = BIGGEST_ALIGNMENT;
770ae6cc
RK
875 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
876 actual_align = (BITS_PER_UNIT
877 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
878 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
9328904c 879 else
770ae6cc
RK
880 actual_align = DECL_OFFSET_ALIGN (field);
881
882 if (known_align != actual_align)
883 layout_decl (field, actual_align);
884
885 /* Now add size of this field to the size of the record. If the size is
886 not constant, treat the field as being a multiple of bytes and just
887 adjust the offset, resetting the bit position. Otherwise, apportion the
888 size amongst the bit position and offset. First handle the case of an
889 unspecified size, which can happen when we have an invalid nested struct
890 definition, such as struct j { struct j { int i; } }. The error message
891 is printed in finish_struct. */
892 if (DECL_SIZE (field) == 0)
893 /* Do nothing. */;
894 else if (! TREE_CONSTANT (DECL_SIZE_UNIT (field)))
9328904c 895 {
770ae6cc
RK
896 rli->offset
897 = size_binop (PLUS_EXPR, rli->offset,
898 convert (sizetype,
899 size_binop (CEIL_DIV_EXPR, rli->bitpos,
900 bitsize_unit_node)));
901 rli->offset
902 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
903 rli->bitpos = bitsize_zero_node;
b1254b72 904 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
9328904c 905 }
9328904c
MM
906 else
907 {
770ae6cc
RK
908 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
909 normalize_rli (rli);
7306ed3f 910 }
9328904c 911}
7306ed3f 912
9328904c
MM
913/* Assuming that all the fields have been laid out, this function uses
914 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
915 inidicated by RLI. */
7306ed3f 916
9328904c
MM
917static void
918finalize_record_size (rli)
919 record_layout_info rli;
920{
770ae6cc
RK
921 tree unpadded_size, unpadded_size_unit;
922
65e14bf5
RK
923 /* Now we want just byte and bit offsets, so set the offset alignment
924 to be a byte and then normalize. */
925 rli->offset_align = BITS_PER_UNIT;
926 normalize_rli (rli);
7306ed3f
JW
927
928 /* Determine the desired alignment. */
929#ifdef ROUND_TYPE_ALIGN
9328904c 930 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
b451555a 931 rli->record_align);
7306ed3f 932#else
9328904c 933 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
7306ed3f 934#endif
11cf4d18 935 TYPE_USER_ALIGN (rli->t) = 1;
7306ed3f 936
65e14bf5
RK
937 /* Compute the size so far. Be sure to allow for extra bits in the
938 size in bytes. We have guaranteed above that it will be no more
939 than a single byte. */
940 unpadded_size = rli_size_so_far (rli);
941 unpadded_size_unit = rli_size_unit_so_far (rli);
942 if (! integer_zerop (rli->bitpos))
943 unpadded_size_unit
944 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
770ae6cc 945
bbc0e641
JM
946 /* Record the un-rounded size in the binfo node. But first we check
947 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
9328904c 948 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
06ceef4e 949 {
770ae6cc
RK
950 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
951 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
06ceef4e 952 }
729a2125 953
770ae6cc 954 /* Round the size up to be a multiple of the required alignment */
7306ed3f 955#ifdef ROUND_TYPE_SIZE
770ae6cc
RK
956 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
957 TYPE_ALIGN (rli->t));
958 TYPE_SIZE_UNIT (rli->t)
c5daf9c4 959 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
770ae6cc 960 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
7306ed3f 961#else
770ae6cc
RK
962 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
963 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
964 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
7306ed3f 965#endif
729a2125 966
770ae6cc
RK
967 if (warn_padded && TREE_CONSTANT (unpadded_size)
968 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
969 warning ("padding struct size to alignment boundary");
3c12fcc2 970
770ae6cc
RK
971 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
972 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
973 && TREE_CONSTANT (unpadded_size))
3c12fcc2
GM
974 {
975 tree unpacked_size;
729a2125 976
3c12fcc2 977#ifdef ROUND_TYPE_ALIGN
9328904c
MM
978 rli->unpacked_align
979 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
3c12fcc2 980#else
9328904c 981 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
3c12fcc2 982#endif
770ae6cc 983
3c12fcc2 984#ifdef ROUND_TYPE_SIZE
9328904c
MM
985 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
986 rli->unpacked_align);
3c12fcc2 987#else
9328904c 988 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
3c12fcc2 989#endif
729a2125 990
9328904c 991 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
3c12fcc2 992 {
770ae6cc
RK
993 TYPE_PACKED (rli->t) = 0;
994
9328904c 995 if (TYPE_NAME (rli->t))
3c12fcc2 996 {
63ad61ed 997 const char *name;
729a2125 998
9328904c
MM
999 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1000 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
3c12fcc2 1001 else
9328904c 1002 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
770ae6cc 1003
3c12fcc2
GM
1004 if (STRICT_ALIGNMENT)
1005 warning ("packed attribute causes inefficient alignment for `%s'", name);
1006 else
1007 warning ("packed attribute is unnecessary for `%s'", name);
1008 }
1009 else
1010 {
1011 if (STRICT_ALIGNMENT)
1012 warning ("packed attribute causes inefficient alignment");
1013 else
1014 warning ("packed attribute is unnecessary");
1015 }
1016 }
3c12fcc2 1017 }
9328904c
MM
1018}
1019
1020/* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
7306ed3f 1021
65e14bf5 1022void
9328904c
MM
1023compute_record_mode (type)
1024 tree type;
1025{
770ae6cc
RK
1026 tree field;
1027 enum machine_mode mode = VOIDmode;
1028
9328904c
MM
1029 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1030 However, if possible, we use a mode that fits in a register
1031 instead, in order to allow for better optimization down the
1032 line. */
1033 TYPE_MODE (type) = BLKmode;
9328904c 1034
770ae6cc
RK
1035 if (! host_integerp (TYPE_SIZE (type), 1))
1036 return;
9328904c 1037
770ae6cc
RK
1038 /* A record which has any BLKmode members must itself be
1039 BLKmode; it can't go in a register. Unless the member is
1040 BLKmode only because it isn't aligned. */
1041 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1042 {
1043 unsigned HOST_WIDE_INT bitpos;
9328904c 1044
770ae6cc
RK
1045 if (TREE_CODE (field) != FIELD_DECL)
1046 continue;
9328904c 1047
770ae6cc
RK
1048 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1049 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1050 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1051 || ! host_integerp (bit_position (field), 1)
1052 || ! host_integerp (DECL_SIZE (field), 1))
1053 return;
1054
1055 bitpos = int_bit_position (field);
1056
1057 /* Must be BLKmode if any field crosses a word boundary,
1058 since extract_bit_field can't handle that in registers. */
1059 if (bitpos / BITS_PER_WORD
1060 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
1061 / BITS_PER_WORD)
1062 /* But there is no problem if the field is entire words. */
1063 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1064 return;
1065
1066 /* If this field is the whole struct, remember its mode so
1067 that, say, we can put a double in a class into a DF
7d2950e5
RK
1068 register instead of forcing it to live in the stack. However,
1069 we don't support using such a mode if there is no integer mode
1070 of the same size, so don't set it here. */
1071 if (field == TYPE_FIELDS (type) && TREE_CHAIN (field) == 0
1072 && int_mode_for_mode (DECL_MODE (field)) != BLKmode)
770ae6cc 1073 mode = DECL_MODE (field);
9328904c
MM
1074
1075#ifdef STRUCT_FORCE_BLK
770ae6cc
RK
1076 /* With some targets, eg. c4x, it is sub-optimal
1077 to access an aligned BLKmode structure as a scalar. */
1078 if (mode == VOIDmode && STRUCT_FORCE_BLK (field))
1079 return;
9328904c 1080#endif /* STRUCT_FORCE_BLK */
770ae6cc 1081 }
9328904c 1082
770ae6cc
RK
1083 if (mode != VOIDmode)
1084 /* We only have one real field; use its mode. */
1085 TYPE_MODE (type) = mode;
1086 else
1087 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1088
1089 /* If structure's known alignment is less than what the scalar
1090 mode would need, and it matters, then stick with BLKmode. */
1091 if (TYPE_MODE (type) != BLKmode
1092 && STRICT_ALIGNMENT
1093 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1094 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1095 {
1096 /* If this is the only reason this type is BLKmode, then
1097 don't force containing types to be BLKmode. */
1098 TYPE_NO_FORCE_BLK (type) = 1;
1099 TYPE_MODE (type) = BLKmode;
9328904c 1100 }
7306ed3f 1101}
9328904c
MM
1102
1103/* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1104 out. */
1105
1106static void
1107finalize_type_size (type)
1108 tree type;
1109{
1110 /* Normally, use the alignment corresponding to the mode chosen.
1111 However, where strict alignment is not required, avoid
1112 over-aligning structures, since most compilers do not do this
1113 alignment. */
1114
1115 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1116 && (STRICT_ALIGNMENT
1117 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1118 && TREE_CODE (type) != QUAL_UNION_TYPE
1119 && TREE_CODE (type) != ARRAY_TYPE)))
11cf4d18
JJ
1120 {
1121 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1122 TYPE_USER_ALIGN (type) = 0;
1123 }
9328904c
MM
1124
1125 /* Do machine-dependent extra alignment. */
1126#ifdef ROUND_TYPE_ALIGN
1127 TYPE_ALIGN (type)
1128 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1129#endif
1130
9328904c 1131 /* If we failed to find a simple way to calculate the unit size
770ae6cc 1132 of the type, find it by division. */
9328904c
MM
1133 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1134 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1135 result will fit in sizetype. We will get more efficient code using
1136 sizetype, so we force a conversion. */
1137 TYPE_SIZE_UNIT (type)
1138 = convert (sizetype,
1139 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
770ae6cc 1140 bitsize_unit_node));
9328904c 1141
770ae6cc
RK
1142 if (TYPE_SIZE (type) != 0)
1143 {
1144#ifdef ROUND_TYPE_SIZE
1145 TYPE_SIZE (type)
1146 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1147 TYPE_SIZE_UNIT (type)
1148 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1149 TYPE_ALIGN (type) / BITS_PER_UNIT);
1150#else
1151 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1152 TYPE_SIZE_UNIT (type)
1153 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1154#endif
1155 }
1156
1157 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1158 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1159 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
9328904c
MM
1160 if (TYPE_SIZE_UNIT (type) != 0
1161 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1162 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1163
1164 /* Also layout any other variants of the type. */
1165 if (TYPE_NEXT_VARIANT (type)
1166 || type != TYPE_MAIN_VARIANT (type))
1167 {
1168 tree variant;
1169 /* Record layout info of this variant. */
1170 tree size = TYPE_SIZE (type);
1171 tree size_unit = TYPE_SIZE_UNIT (type);
1172 unsigned int align = TYPE_ALIGN (type);
11cf4d18 1173 unsigned int user_align = TYPE_USER_ALIGN (type);
9328904c
MM
1174 enum machine_mode mode = TYPE_MODE (type);
1175
1176 /* Copy it into all variants. */
1177 for (variant = TYPE_MAIN_VARIANT (type);
1178 variant != 0;
1179 variant = TYPE_NEXT_VARIANT (variant))
1180 {
1181 TYPE_SIZE (variant) = size;
1182 TYPE_SIZE_UNIT (variant) = size_unit;
1183 TYPE_ALIGN (variant) = align;
11cf4d18 1184 TYPE_USER_ALIGN (variant) = user_align;
9328904c
MM
1185 TYPE_MODE (variant) = mode;
1186 }
1187 }
1188}
1189
1190/* Do all of the work required to layout the type indicated by RLI,
1191 once the fields have been laid out. This function will call `free'
1192 for RLI. */
1193
1194void
1195finish_record_layout (rli)
1196 record_layout_info rli;
1197{
770ae6cc
RK
1198 /* Compute the final size. */
1199 finalize_record_size (rli);
1200
1201 /* Compute the TYPE_MODE for the record. */
1202 compute_record_mode (rli->t);
cc9d4a85 1203
8d8238b6
JM
1204 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1205 finalize_type_size (rli->t);
1206
9328904c
MM
1207 /* Lay out any static members. This is done now because their type
1208 may use the record's type. */
1209 while (rli->pending_statics)
1210 {
1211 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1212 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1213 }
cc9d4a85 1214
9328904c
MM
1215 /* Clean up. */
1216 free (rli);
1217}
7306ed3f
JW
1218\f
1219/* Calculate the mode, size, and alignment for TYPE.
1220 For an array type, calculate the element separation as well.
1221 Record TYPE on the chain of permanent or temporary types
1222 so that dbxout will find out about it.
1223
1224 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1225 layout_type does nothing on such a type.
1226
1227 If the type is incomplete, its TYPE_SIZE remains zero. */
1228
1229void
1230layout_type (type)
1231 tree type;
1232{
7306ed3f
JW
1233 if (type == 0)
1234 abort ();
1235
1236 /* Do nothing if type has been laid out before. */
1237 if (TYPE_SIZE (type))
1238 return;
1239
7306ed3f
JW
1240 switch (TREE_CODE (type))
1241 {
1242 case LANG_TYPE:
1243 /* This kind of type is the responsibility
9faa82d8 1244 of the language-specific code. */
7306ed3f
JW
1245 abort ();
1246
e9a25f70
JL
1247 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1248 if (TYPE_PRECISION (type) == 0)
1249 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
d4b60170 1250
e9a25f70
JL
1251 /* ... fall through ... */
1252
7306ed3f
JW
1253 case INTEGER_TYPE:
1254 case ENUMERAL_TYPE:
fc69eca0 1255 case CHAR_TYPE:
e2a77f99
RK
1256 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1257 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
7306ed3f
JW
1258 TREE_UNSIGNED (type) = 1;
1259
5e9bec99
RK
1260 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1261 MODE_INT);
06ceef4e 1262 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
ead17059 1263 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
7306ed3f
JW
1264 break;
1265
1266 case REAL_TYPE:
1267 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
06ceef4e 1268 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
ead17059 1269 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
7306ed3f
JW
1270 break;
1271
1272 case COMPLEX_TYPE:
1273 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1274 TYPE_MODE (type)
1275 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1276 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1277 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1278 0);
06ceef4e 1279 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
ead17059 1280 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
7306ed3f
JW
1281 break;
1282
0b4565c9
BS
1283 case VECTOR_TYPE:
1284 {
1285 tree subtype;
1286
1287 subtype = TREE_TYPE (type);
1288 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1289 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1290 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1291 }
1292 break;
1293
7306ed3f 1294 case VOID_TYPE:
770ae6cc 1295 /* This is an incomplete type and so doesn't have a size. */
7306ed3f 1296 TYPE_ALIGN (type) = 1;
11cf4d18 1297 TYPE_USER_ALIGN (type) = 0;
7306ed3f
JW
1298 TYPE_MODE (type) = VOIDmode;
1299 break;
1300
321cb743 1301 case OFFSET_TYPE:
06ceef4e 1302 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
ead17059 1303 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
9fe0563a 1304 TYPE_MODE (type) = ptr_mode;
321cb743
MT
1305 break;
1306
7306ed3f
JW
1307 case FUNCTION_TYPE:
1308 case METHOD_TYPE:
13275056 1309 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
06ceef4e 1310 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
ead17059 1311 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
7306ed3f
JW
1312 break;
1313
1314 case POINTER_TYPE:
1315 case REFERENCE_TYPE:
9fe0563a 1316 TYPE_MODE (type) = ptr_mode;
06ceef4e 1317 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
ead17059 1318 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
7306ed3f 1319 TREE_UNSIGNED (type) = 1;
13275056 1320 TYPE_PRECISION (type) = POINTER_SIZE;
7306ed3f
JW
1321 break;
1322
1323 case ARRAY_TYPE:
1324 {
1325 register tree index = TYPE_DOMAIN (type);
1326 register tree element = TREE_TYPE (type);
1327
1328 build_pointer_type (element);
1329
1330 /* We need to know both bounds in order to compute the size. */
1331 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1332 && TYPE_SIZE (element))
1333 {
e24ff973
RK
1334 tree ub = TYPE_MAX_VALUE (index);
1335 tree lb = TYPE_MIN_VALUE (index);
1336 tree length;
74a4fbfc 1337 tree element_size;
e24ff973
RK
1338
1339 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
1340 test for negative below covers it. */
1341 if (TREE_CODE (ub) == MAX_EXPR
1342 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
1343 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
1344 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
1345 lb, 0))
1346 ub = TREE_OPERAND (ub, 1);
1347 else if (TREE_CODE (ub) == MAX_EXPR
1348 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
1349 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
1350 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
1351 0),
1352 lb, 0))
1353 ub = TREE_OPERAND (ub, 0);
1354
a2d53b28
RH
1355 /* The initial subtraction should happen in the original type so
1356 that (possible) negative values are handled appropriately. */
e24ff973 1357 length = size_binop (PLUS_EXPR, size_one_node,
fed3cef0
RK
1358 convert (sizetype,
1359 fold (build (MINUS_EXPR,
1360 TREE_TYPE (lb),
1361 ub, lb))));
7306ed3f 1362
36427b78
RK
1363 /* If neither bound is a constant and sizetype is signed, make
1364 sure the size is never negative. We should really do this
1365 if *either* bound is non-constant, but this is the best
1366 compromise between C and Ada. */
1367 if (! TREE_UNSIGNED (sizetype)
1368 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1369 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1370 length = size_binop (MAX_EXPR, length, size_zero_node);
1371
74a4fbfc
DB
1372 /* Special handling for arrays of bits (for Chill). */
1373 element_size = TYPE_SIZE (element);
1374 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
1375 {
d4b60170
RK
1376 HOST_WIDE_INT maxvalue
1377 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
1378 HOST_WIDE_INT minvalue
1379 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
1380
74a4fbfc
DB
1381 if (maxvalue - minvalue == 1
1382 && (maxvalue == 1 || maxvalue == 0))
1383 element_size = integer_one_node;
1384 }
1385
fed3cef0
RK
1386 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1387 convert (bitsizetype, length));
ead17059
RH
1388
1389 /* If we know the size of the element, calculate the total
1390 size directly, rather than do some division thing below.
1391 This optimization helps Fortran assumed-size arrays
1392 (where the size of the array is determined at runtime)
7771032e
DB
1393 substantially.
1394 Note that we can't do this in the case where the size of
1395 the elements is one bit since TYPE_SIZE_UNIT cannot be
1396 set correctly in that case. */
fed3cef0 1397 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
d4b60170
RK
1398 TYPE_SIZE_UNIT (type)
1399 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
7306ed3f
JW
1400 }
1401
1402 /* Now round the alignment and size,
1403 using machine-dependent criteria if any. */
1404
1405#ifdef ROUND_TYPE_ALIGN
1406 TYPE_ALIGN (type)
1407 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1408#else
1409 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1410#endif
1411
1412#ifdef ROUND_TYPE_SIZE
1413 if (TYPE_SIZE (type) != 0)
ead17059 1414 {
d4b60170
RK
1415 tree tmp
1416 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1417
ead17059
RH
1418 /* If the rounding changed the size of the type, remove any
1419 pre-calculated TYPE_SIZE_UNIT. */
1420 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1421 TYPE_SIZE_UNIT (type) = NULL;
d4b60170 1422
ead17059
RH
1423 TYPE_SIZE (type) = tmp;
1424 }
7306ed3f
JW
1425#endif
1426
1427 TYPE_MODE (type) = BLKmode;
1428 if (TYPE_SIZE (type) != 0
7306ed3f
JW
1429 /* BLKmode elements force BLKmode aggregate;
1430 else extract/store fields may lose. */
1431 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1432 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1433 {
1434 TYPE_MODE (type)
72c602fc 1435 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
7306ed3f 1436
72c602fc
RK
1437 if (TYPE_MODE (type) != BLKmode
1438 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1439 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
7306ed3f
JW
1440 && TYPE_MODE (type) != BLKmode)
1441 {
1442 TYPE_NO_FORCE_BLK (type) = 1;
1443 TYPE_MODE (type) = BLKmode;
1444 }
7306ed3f
JW
1445 }
1446 break;
1447 }
1448
1449 case RECORD_TYPE:
cc9d4a85
MM
1450 case UNION_TYPE:
1451 case QUAL_UNION_TYPE:
9328904c
MM
1452 {
1453 tree field;
1454 record_layout_info rli;
1455
1456 /* Initialize the layout information. */
770ae6cc
RK
1457 rli = start_record_layout (type);
1458
cc9d4a85
MM
1459 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1460 in the reverse order in building the COND_EXPR that denotes
1461 its size. We reverse them again later. */
1462 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1463 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
770ae6cc
RK
1464
1465 /* Place all the fields. */
9328904c 1466 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
770ae6cc
RK
1467 place_field (rli, field);
1468
cc9d4a85
MM
1469 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1470 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
770ae6cc 1471
9328904c
MM
1472 /* Finish laying out the record. */
1473 finish_record_layout (rli);
1474 }
7306ed3f
JW
1475 break;
1476
e9a25f70 1477 case SET_TYPE: /* Used by Chill and Pascal. */
b5d11e41
PB
1478 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1479 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1480 abort();
1481 else
1482 {
1483#ifndef SET_WORD_SIZE
1484#define SET_WORD_SIZE BITS_PER_WORD
1485#endif
729a2125
RK
1486 unsigned int alignment
1487 = set_alignment ? set_alignment : SET_WORD_SIZE;
db3cf6fb
MS
1488 int size_in_bits
1489 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1490 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
b5d11e41
PB
1491 int rounded_size
1492 = ((size_in_bits + alignment - 1) / alignment) * alignment;
729a2125
RK
1493
1494 if (rounded_size > (int) alignment)
b5d11e41
PB
1495 TYPE_MODE (type) = BLKmode;
1496 else
1497 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
729a2125 1498
06ceef4e 1499 TYPE_SIZE (type) = bitsize_int (rounded_size);
ead17059 1500 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
b5d11e41 1501 TYPE_ALIGN (type) = alignment;
11cf4d18 1502 TYPE_USER_ALIGN (type) = 0;
b5d11e41
PB
1503 TYPE_PRECISION (type) = size_in_bits;
1504 }
1505 break;
1506
4cc89e53
RS
1507 case FILE_TYPE:
1508 /* The size may vary in different languages, so the language front end
1509 should fill in the size. */
1510 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
11cf4d18 1511 TYPE_USER_ALIGN (type) = 0;
4cc89e53
RS
1512 TYPE_MODE (type) = BLKmode;
1513 break;
1514
7306ed3f
JW
1515 default:
1516 abort ();
729a2125 1517 }
7306ed3f 1518
9328904c 1519 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
cc9d4a85
MM
1520 records and unions, finish_record_layout already called this
1521 function. */
1522 if (TREE_CODE (type) != RECORD_TYPE
1523 && TREE_CODE (type) != UNION_TYPE
1524 && TREE_CODE (type) != QUAL_UNION_TYPE)
9328904c 1525 finalize_type_size (type);
7306ed3f 1526
fed3cef0
RK
1527 /* If this type is created before sizetype has been permanently set,
1528 record it so set_sizetype can fix it up. */
1529 if (! sizetype_set)
ad41cc2a 1530 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
7306ed3f
JW
1531}
1532\f
1533/* Create and return a type for signed integers of PRECISION bits. */
1534
1535tree
1536make_signed_type (precision)
1537 int precision;
1538{
1539 register tree type = make_node (INTEGER_TYPE);
1540
1541 TYPE_PRECISION (type) = precision;
1542
fed3cef0 1543 fixup_signed_type (type);
7306ed3f
JW
1544 return type;
1545}
1546
1547/* Create and return a type for unsigned integers of PRECISION bits. */
1548
1549tree
1550make_unsigned_type (precision)
1551 int precision;
1552{
1553 register tree type = make_node (INTEGER_TYPE);
1554
1555 TYPE_PRECISION (type) = precision;
1556
7306ed3f
JW
1557 fixup_unsigned_type (type);
1558 return type;
1559}
fed3cef0
RK
1560\f
1561/* Initialize sizetype and bitsizetype to a reasonable and temporary
1562 value to enable integer types to be created. */
1563
1564void
1565initialize_sizetypes ()
1566{
1567 tree t = make_node (INTEGER_TYPE);
1568
1569 /* Set this so we do something reasonable for the build_int_2 calls
1570 below. */
1571 integer_type_node = t;
1572
1573 TYPE_MODE (t) = SImode;
1574 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
11cf4d18 1575 TYPE_USER_ALIGN (t) = 0;
fed3cef0
RK
1576 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1577 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1578 TREE_UNSIGNED (t) = 1;
1579 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1580 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
770ae6cc 1581 TYPE_IS_SIZETYPE (t) = 1;
fed3cef0
RK
1582
1583 /* 1000 avoids problems with possible overflow and is certainly
1584 larger than any size value we'd want to be storing. */
1585 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1586
1587 /* These two must be different nodes because of the caching done in
1588 size_int_wide. */
1589 sizetype = t;
1590 bitsizetype = copy_node (t);
1591 integer_type_node = 0;
1592}
7306ed3f 1593
896cced4 1594/* Set sizetype to TYPE, and initialize *sizetype accordingly.
f8dac6eb
R
1595 Also update the type of any standard type's sizes made so far. */
1596
1597void
1598set_sizetype (type)
1599 tree type;
1600{
d4b60170 1601 int oprecision = TYPE_PRECISION (type);
f8dac6eb 1602 /* The *bitsizetype types use a precision that avoids overflows when
d4b60170
RK
1603 calculating signed sizes / offsets in bits. However, when
1604 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1605 precision. */
11a6092b 1606 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
d4b60170 1607 2 * HOST_BITS_PER_WIDE_INT);
fed3cef0 1608 unsigned int i;
ad41cc2a 1609 tree t;
fed3cef0
RK
1610
1611 if (sizetype_set)
1612 abort ();
81b3411c 1613
fed3cef0
RK
1614 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1615 sizetype = copy_node (type);
21318741 1616 TYPE_DOMAIN (sizetype) = type;
770ae6cc 1617 TYPE_IS_SIZETYPE (sizetype) = 1;
81b3411c
BS
1618 bitsizetype = make_node (INTEGER_TYPE);
1619 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
f8dac6eb 1620 TYPE_PRECISION (bitsizetype) = precision;
770ae6cc 1621 TYPE_IS_SIZETYPE (bitsizetype) = 1;
d4b60170 1622
896cced4
RH
1623 if (TREE_UNSIGNED (type))
1624 fixup_unsigned_type (bitsizetype);
1625 else
1626 fixup_signed_type (bitsizetype);
d4b60170 1627
f8dac6eb
R
1628 layout_type (bitsizetype);
1629
896cced4
RH
1630 if (TREE_UNSIGNED (type))
1631 {
1632 usizetype = sizetype;
1633 ubitsizetype = bitsizetype;
fed3cef0
RK
1634 ssizetype = copy_node (make_signed_type (oprecision));
1635 sbitsizetype = copy_node (make_signed_type (precision));
896cced4
RH
1636 }
1637 else
1638 {
1639 ssizetype = sizetype;
1640 sbitsizetype = bitsizetype;
fed3cef0
RK
1641 usizetype = copy_node (make_unsigned_type (oprecision));
1642 ubitsizetype = copy_node (make_unsigned_type (precision));
896cced4 1643 }
fed3cef0
RK
1644
1645 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1646
21318741 1647 /* Show is a sizetype, is a main type, and has no pointers to it. */
b6a1cbae 1648 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
21318741
RK
1649 {
1650 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1651 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1652 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1653 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1654 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1655 }
d7db6646 1656
d4b60170
RK
1657 ggc_add_tree_root ((tree *) &sizetype_tab,
1658 sizeof sizetype_tab / sizeof (tree));
f8dac6eb 1659
fed3cef0
RK
1660 /* Go down each of the types we already made and set the proper type
1661 for the sizes in them. */
ad41cc2a 1662 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
fed3cef0 1663 {
ad41cc2a 1664 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
fed3cef0
RK
1665 abort ();
1666
ad41cc2a
RK
1667 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1668 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
fed3cef0
RK
1669 }
1670
1671 early_type_list = 0;
1672 sizetype_set = 1;
1673}
1674\f
4cc89e53 1675/* Set the extreme values of TYPE based on its precision in bits,
13756074 1676 then lay it out. Used when make_signed_type won't do
4cc89e53
RS
1677 because the tree code is not INTEGER_TYPE.
1678 E.g. for Pascal, when the -fsigned-char option is given. */
1679
1680void
1681fixup_signed_type (type)
1682 tree type;
1683{
1684 register int precision = TYPE_PRECISION (type);
1685
1686 TYPE_MIN_VALUE (type)
13756074
JW
1687 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1688 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1689 (((HOST_WIDE_INT) (-1)
1690 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1691 ? precision - HOST_BITS_PER_WIDE_INT - 1
1692 : 0))));
4cc89e53 1693 TYPE_MAX_VALUE (type)
13756074
JW
1694 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1695 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1696 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1697 ? (((HOST_WIDE_INT) 1
1698 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
4cc89e53
RS
1699 : 0));
1700
1701 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1702 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1703
1704 /* Lay out the type: set its alignment, size, etc. */
4cc89e53
RS
1705 layout_type (type);
1706}
1707
7306ed3f 1708/* Set the extreme values of TYPE based on its precision in bits,
13756074 1709 then lay it out. This is used both in `make_unsigned_type'
7306ed3f
JW
1710 and for enumeral types. */
1711
1712void
1713fixup_unsigned_type (type)
1714 tree type;
1715{
1716 register int precision = TYPE_PRECISION (type);
1717
1718 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1719 TYPE_MAX_VALUE (type)
c166a311 1720 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
13756074 1721 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
c166a311
CH
1722 precision - HOST_BITS_PER_WIDE_INT > 0
1723 ? ((unsigned HOST_WIDE_INT) ~0
1724 >> (HOST_BITS_PER_WIDE_INT
1725 - (precision - HOST_BITS_PER_WIDE_INT)))
7306ed3f
JW
1726 : 0);
1727 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1728 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1729
1730 /* Lay out the type: set its alignment, size, etc. */
7306ed3f
JW
1731 layout_type (type);
1732}
1733\f
1734/* Find the best machine mode to use when referencing a bit field of length
1735 BITSIZE bits starting at BITPOS.
1736
1737 The underlying object is known to be aligned to a boundary of ALIGN bits.
1738 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1739 larger than LARGEST_MODE (usually SImode).
1740
1741 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1742 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1743 mode meeting these conditions.
1744
77fa0940
RK
1745 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1746 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1747 all the conditions. */
7306ed3f
JW
1748
1749enum machine_mode
1750get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1751 int bitsize, bitpos;
729a2125 1752 unsigned int align;
7306ed3f
JW
1753 enum machine_mode largest_mode;
1754 int volatilep;
1755{
1756 enum machine_mode mode;
770ae6cc 1757 unsigned int unit = 0;
7306ed3f
JW
1758
1759 /* Find the narrowest integer mode that contains the bit field. */
1760 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1761 mode = GET_MODE_WIDER_MODE (mode))
1762 {
1763 unit = GET_MODE_BITSIZE (mode);
956d6950 1764 if ((bitpos % unit) + bitsize <= unit)
7306ed3f
JW
1765 break;
1766 }
1767
0c61f541 1768 if (mode == VOIDmode
7306ed3f 1769 /* It is tempting to omit the following line
4e4b555d 1770 if STRICT_ALIGNMENT is true.
7306ed3f
JW
1771 But that is incorrect, since if the bitfield uses part of 3 bytes
1772 and we use a 4-byte mode, we could get a spurious segv
1773 if the extra 4th byte is past the end of memory.
1774 (Though at least one Unix compiler ignores this problem:
1775 that on the Sequent 386 machine. */
770ae6cc 1776 || MIN (unit, BIGGEST_ALIGNMENT) > align
7306ed3f
JW
1777 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1778 return VOIDmode;
1779
77fa0940
RK
1780 if (SLOW_BYTE_ACCESS && ! volatilep)
1781 {
1782 enum machine_mode wide_mode = VOIDmode, tmode;
1783
1784 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1785 tmode = GET_MODE_WIDER_MODE (tmode))
1786 {
1787 unit = GET_MODE_BITSIZE (tmode);
1788 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1789 && unit <= BITS_PER_WORD
770ae6cc 1790 && unit <= MIN (align, BIGGEST_ALIGNMENT)
77fa0940
RK
1791 && (largest_mode == VOIDmode
1792 || unit <= GET_MODE_BITSIZE (largest_mode)))
1793 wide_mode = tmode;
1794 }
1795
1796 if (wide_mode != VOIDmode)
1797 return wide_mode;
1798 }
7306ed3f
JW
1799
1800 return mode;
1801}
d7db6646 1802
36c265b1
NS
1803/* Return the alignment of MODE. This will be bounded by 1 and
1804 BIGGEST_ALIGNMENT. */
1805
fed3cef0
RK
1806unsigned int
1807get_mode_alignment (mode)
36c265b1
NS
1808 enum machine_mode mode;
1809{
19caa751 1810 unsigned int alignment = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
36c265b1
NS
1811
1812 /* Extract the LSB of the size. */
1813 alignment = alignment & -alignment;
36c265b1
NS
1814
1815 alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1816 return alignment;
1817}
1818
d7db6646
RH
1819/* This function is run once to initialize stor-layout.c. */
1820
1821void
1822init_stor_layout_once ()
1823{
1824 ggc_add_tree_root (&pending_sizes, 1);
1825}