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