]> gcc.gnu.org Git - gcc.git/blob - gcc/stor-layout.c
(variable_size): Better error for div by 0 in size.
[gcc.git] / gcc / stor-layout.c
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include "config.h"
22 #include <stdio.h>
23
24 #include "tree.h"
25 #include "function.h"
26
27 #define CEIL(x,y) (((x) + (y) - 1) / (y))
28
29 /* Data type for the expressions representing sizes of data types.
30 It is the first integer type laid out.
31 In C, this is int. */
32
33 tree sizetype;
34
35 /* An integer constant with value 0 whose type is sizetype. */
36
37 tree size_zero_node;
38
39 /* An integer constant with value 1 whose type is sizetype. */
40
41 tree size_one_node;
42
43 /* If nonzero, this is an upper limit on alignment of structure fields.
44 The value is measured in bits. */
45 int maximum_field_alignment;
46
47 #define GET_MODE_ALIGNMENT(MODE) \
48 MIN (BIGGEST_ALIGNMENT, \
49 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
50 \f
51 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
52
53 static tree pending_sizes;
54
55 /* Nonzero means cannot safely call expand_expr now,
56 so put variable sizes onto `pending_sizes' instead. */
57
58 int immediate_size_expand;
59
60 tree
61 get_pending_sizes ()
62 {
63 tree chain = pending_sizes;
64 tree t;
65
66 /* Put each SAVE_EXPR into the current function. */
67 for (t = chain; t; t = TREE_CHAIN (t))
68 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
69 pending_sizes = 0;
70 return chain;
71 }
72
73 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
74 to serve as the actual size-expression for a type or decl. */
75
76 tree
77 variable_size (size)
78 tree size;
79 {
80 size = save_expr (size);
81
82 if (global_bindings_p ())
83 {
84 if (TREE_CONSTANT (size))
85 error ("type size can't be explicitly evaluated");
86 else
87 error ("variable-size type declared outside of any function");
88
89 return size_int (1);
90 }
91
92 if (immediate_size_expand)
93 /* NULL_RTX is not defined; neither is the rtx type. */
94 expand_expr (size, NULL_PTR, VOIDmode, 0);
95 else
96 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
97
98 return size;
99 }
100 \f
101 #ifndef MAX_FIXED_MODE_SIZE
102 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
103 #endif
104
105 /* Return the machine mode to use for a nonscalar of SIZE bits.
106 The mode must be in class CLASS, and have exactly that many bits.
107 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
108 be used. */
109
110 enum machine_mode
111 mode_for_size (size, class, limit)
112 unsigned int size;
113 enum mode_class class;
114 int limit;
115 {
116 register enum machine_mode mode;
117
118 if (limit && size > MAX_FIXED_MODE_SIZE)
119 return BLKmode;
120
121 /* Get the last mode which has this size, in the specified class. */
122 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
123 mode = GET_MODE_WIDER_MODE (mode))
124 if (GET_MODE_BITSIZE (mode) == size)
125 return mode;
126
127 return BLKmode;
128 }
129
130 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
131
132 tree
133 round_up (value, divisor)
134 tree value;
135 int divisor;
136 {
137 return size_binop (MULT_EXPR,
138 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
139 size_int (divisor));
140 }
141 \f
142 /* Set the size, mode and alignment of a ..._DECL node.
143 TYPE_DECL does need this for C++.
144 Note that LABEL_DECL and CONST_DECL nodes do not need this,
145 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
146 Don't call layout_decl for them.
147
148 KNOWN_ALIGN is the amount of alignment we can assume this
149 decl has with no special effort. It is relevant only for FIELD_DECLs
150 and depends on the previous fields.
151 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
152 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
153 the record will be aligned to suit. */
154
155 void
156 layout_decl (decl, known_align)
157 tree decl;
158 unsigned known_align;
159 {
160 register tree type = TREE_TYPE (decl);
161 register enum tree_code code = TREE_CODE (decl);
162 int spec_size = DECL_FIELD_SIZE (decl);
163
164 if (code == CONST_DECL)
165 return;
166
167 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
168 && code != FIELD_DECL && code != TYPE_DECL)
169 abort ();
170
171 if (type == error_mark_node)
172 {
173 type = void_type_node;
174 spec_size = 0;
175 }
176
177 /* Usually the size and mode come from the data type without change. */
178
179 DECL_MODE (decl) = TYPE_MODE (type);
180 DECL_SIZE (decl) = TYPE_SIZE (type);
181 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
182
183 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
184 {
185 /* This is a bit-field. We don't know how to handle
186 them except for integers and enums, and front end should
187 never generate them otherwise. */
188
189 if (! (TREE_CODE (type) == INTEGER_TYPE
190 || TREE_CODE (type) == ENUMERAL_TYPE))
191 abort ();
192
193 if (spec_size == 0 && DECL_NAME (decl) != 0)
194 abort ();
195
196 /* Size is specified number of bits. */
197 DECL_SIZE (decl) = size_int (spec_size);
198 }
199 /* Force alignment required for the data type.
200 But if the decl itself wants greater alignment, don't override that.
201 Likewise, if the decl is packed, don't override it. */
202 else if (DECL_ALIGN (decl) == 0
203 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
204 DECL_ALIGN (decl) = TYPE_ALIGN (type);
205
206 /* See if we can use an ordinary integer mode for a bit-field. */
207 /* Conditions are: a fixed size that is correct for another mode
208 and occupying a complete byte or bytes on proper boundary. */
209 if (code == FIELD_DECL)
210 {
211 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
212 if (maximum_field_alignment != 0)
213 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
214 }
215
216 if (DECL_BIT_FIELD (decl)
217 && TYPE_SIZE (type) != 0
218 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
219 {
220 register enum machine_mode xmode
221 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
222
223 if (xmode != BLKmode
224 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
225 {
226 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
227 DECL_ALIGN (decl));
228 DECL_MODE (decl) = xmode;
229 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
230 /* This no longer needs to be accessed as a bit field. */
231 DECL_BIT_FIELD (decl) = 0;
232 }
233 }
234
235 /* Evaluate nonconstant size only once, either now or as soon as safe. */
236 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
237 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
238 }
239 \f
240 /* Lay out a RECORD_TYPE type (a C struct).
241 This means laying out the fields, determining their positions,
242 and computing the overall size and required alignment of the record.
243 Note that if you set the TYPE_ALIGN before calling this
244 then the struct is aligned to at least that boundary.
245
246 If the type has basetypes, you must call layout_basetypes
247 before calling this function.
248
249 The return value is a list of static members of the record.
250 They still need to be laid out. */
251
252 static tree
253 layout_record (rec)
254 tree rec;
255 {
256 register tree field;
257 #ifdef STRUCTURE_SIZE_BOUNDARY
258 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
259 #else
260 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
261 #endif
262 /* These must be laid out *after* the record is. */
263 tree pending_statics = NULL_TREE;
264 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
265 where CONST_SIZE is an integer
266 and VAR_SIZE is a tree expression.
267 If VAR_SIZE is null, the size is just CONST_SIZE.
268 Naturally we try to avoid using VAR_SIZE. */
269 register int const_size = 0;
270 register tree var_size = 0;
271 /* Once we start using VAR_SIZE, this is the maximum alignment
272 that we know VAR_SIZE has. */
273 register int var_align = BITS_PER_UNIT;
274
275
276 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
277 {
278 register int desired_align;
279
280 /* If FIELD is static, then treat it like a separate variable,
281 not really like a structure field.
282 If it is a FUNCTION_DECL, it's a method.
283 In both cases, all we do is lay out the decl,
284 and we do it *after* the record is laid out. */
285
286 if (TREE_STATIC (field))
287 {
288 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
289 continue;
290 }
291 /* Enumerators and enum types which are local to this class need not
292 be laid out. Likewise for initialized constant fields. */
293 if (TREE_CODE (field) != FIELD_DECL)
294 continue;
295
296 /* Lay out the field so we know what alignment it needs.
297 For KNOWN_ALIGN, pass the number of bits from start of record
298 or some divisor of it. */
299
300 /* For a packed field, use the alignment as specified,
301 disregarding what the type would want. */
302 if (DECL_PACKED (field))
303 desired_align = DECL_ALIGN (field);
304 layout_decl (field, var_size ? var_align : const_size);
305 if (! DECL_PACKED (field))
306 desired_align = DECL_ALIGN (field);
307 /* Some targets (i.e. VMS) limit struct field alignment
308 to a lower boundary than alignment of variables. */
309 #ifdef BIGGEST_FIELD_ALIGNMENT
310 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
311 #endif
312
313 /* Record must have at least as much alignment as any field.
314 Otherwise, the alignment of the field within the record
315 is meaningless. */
316
317 #ifndef PCC_BITFIELD_TYPE_MATTERS
318 record_align = MAX (record_align, desired_align);
319 #else
320 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
321 && DECL_BIT_FIELD_TYPE (field)
322 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
323 {
324 /* For these machines, a zero-length field does not
325 affect the alignment of the structure as a whole.
326 It does, however, affect the alignment of the next field
327 within the structure. */
328 if (! integer_zerop (DECL_SIZE (field)))
329 record_align = MAX (record_align, desired_align);
330 else if (! DECL_PACKED (field))
331 desired_align = TYPE_ALIGN (TREE_TYPE (field));
332 /* A named bit field of declared type `int'
333 forces the entire structure to have `int' alignment. */
334 if (DECL_NAME (field) != 0)
335 {
336 int type_align = TYPE_ALIGN (TREE_TYPE (field));
337 if (maximum_field_alignment != 0)
338 type_align = MIN (type_align, maximum_field_alignment);
339
340 record_align = MAX (record_align, type_align);
341 }
342 }
343 else
344 record_align = MAX (record_align, desired_align);
345 #endif
346
347 /* Does this field automatically have alignment it needs
348 by virtue of the fields that precede it and the record's
349 own alignment? */
350
351 if (const_size % desired_align != 0
352 || (var_align % desired_align != 0
353 && var_size != 0))
354 {
355 /* No, we need to skip space before this field.
356 Bump the cumulative size to multiple of field alignment. */
357
358 if (var_size == 0
359 || var_align % desired_align == 0)
360 const_size
361 = CEIL (const_size, desired_align) * desired_align;
362 else
363 {
364 if (const_size > 0)
365 var_size = size_binop (PLUS_EXPR, var_size,
366 size_int (const_size));
367 const_size = 0;
368 var_size = round_up (var_size, desired_align);
369 var_align = MIN (var_align, desired_align);
370 }
371 }
372
373 #ifdef PCC_BITFIELD_TYPE_MATTERS
374 if (PCC_BITFIELD_TYPE_MATTERS
375 && TREE_CODE (field) == FIELD_DECL
376 && TREE_TYPE (field) != error_mark_node
377 && DECL_BIT_FIELD_TYPE (field)
378 && !DECL_PACKED (field)
379 && !integer_zerop (DECL_SIZE (field)))
380 {
381 int type_align = TYPE_ALIGN (TREE_TYPE (field));
382 register tree dsize = DECL_SIZE (field);
383 int field_size = TREE_INT_CST_LOW (dsize);
384
385 if (maximum_field_alignment != 0)
386 type_align = MIN (type_align, maximum_field_alignment);
387
388 /* A bit field may not span the unit of alignment of its type.
389 Advance to next boundary if necessary. */
390 /* ??? There is some uncertainty here as to what
391 should be done if type_align is less than the width of the type.
392 That can happen because the width exceeds BIGGEST_ALIGNMENT
393 or because it exceeds maximum_field_alignment. */
394 if (const_size / type_align
395 != (const_size + field_size - 1) / type_align)
396 const_size = CEIL (const_size, type_align) * type_align;
397 }
398 #endif
399
400 /* No existing machine description uses this parameter.
401 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
402 #ifdef BITFIELD_NBYTES_LIMITED
403 if (BITFIELD_NBYTES_LIMITED
404 && TREE_CODE (field) == FIELD_DECL
405 && TREE_TYPE (field) != error_mark_node
406 && DECL_BIT_FIELD_TYPE (field)
407 && !DECL_PACKED (field)
408 && !integer_zerop (DECL_SIZE (field)))
409 {
410 int type_align = TYPE_ALIGN (TREE_TYPE (field));
411 register tree dsize = DECL_SIZE (field);
412 int field_size = TREE_INT_CST_LOW (dsize);
413
414 if (maximum_field_alignment != 0)
415 type_align = MIN (type_align, maximum_field_alignment);
416
417 /* A bit field may not span the unit of alignment of its type.
418 Advance to next boundary if necessary. */
419 if (const_size / type_align
420 != (const_size + field_size - 1) / type_align)
421 const_size = CEIL (const_size, type_align) * type_align;
422 }
423 #endif
424
425 /* Size so far becomes the position of this field. */
426
427 if (var_size && const_size)
428 DECL_FIELD_BITPOS (field)
429 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
430 else if (var_size)
431 DECL_FIELD_BITPOS (field) = var_size;
432 else
433 DECL_FIELD_BITPOS (field) = size_int (const_size);
434
435 /* If this field is an anonymous union,
436 give each union-member the same position as the union has. */
437
438 if (DECL_NAME (field) == 0
439 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
440 {
441 tree uelt = TYPE_FIELDS (TREE_TYPE (field));
442 for (; uelt; uelt = TREE_CHAIN (uelt))
443 {
444 DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
445 DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
446 }
447 }
448
449 /* Now add size of this field to the size of the record. */
450
451 {
452 register tree dsize = DECL_SIZE (field);
453
454 /* This can happen when we have an invalid nested struct definition,
455 such as struct j { struct j { int i; } }. The error message is
456 printed in finish_struct. */
457 if (dsize == 0)
458 /* Do nothing. */;
459 else if (TREE_CODE (dsize) == INTEGER_CST
460 && TREE_INT_CST_HIGH (dsize) == 0
461 && TREE_INT_CST_LOW (dsize) + const_size > const_size)
462 /* Use const_size if there's no overflow. */
463 const_size += TREE_INT_CST_LOW (dsize);
464 else
465 {
466 if (var_size == 0)
467 var_size = dsize;
468 else
469 var_size = size_binop (PLUS_EXPR, var_size, dsize);
470 }
471 }
472 }
473
474 /* Work out the total size and alignment of the record
475 as one expression and store in the record type.
476 Round it up to a multiple of the record's alignment. */
477
478 if (var_size == 0)
479 {
480 TYPE_SIZE (rec) = size_int (const_size);
481 }
482 else
483 {
484 if (const_size)
485 var_size
486 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
487 TYPE_SIZE (rec) = var_size;
488 }
489
490 /* Determine the desired alignment. */
491 #ifdef ROUND_TYPE_ALIGN
492 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
493 #else
494 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
495 #endif
496
497 #ifdef ROUND_TYPE_SIZE
498 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
499 #else
500 /* Round the size up to be a multiple of the required alignment */
501 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
502 #endif
503
504 return pending_statics;
505 }
506 \f
507 /* Lay out a UNION_TYPE type.
508 Lay out all the fields, set their positions to zero,
509 and compute the size and alignment of the union (maximum of any field).
510 Note that if you set the TYPE_ALIGN before calling this
511 then the union align is aligned to at least that boundary. */
512
513 static void
514 layout_union (rec)
515 tree rec;
516 {
517 register tree field;
518 #ifdef STRUCTURE_SIZE_BOUNDARY
519 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
520 #else
521 unsigned union_align = BITS_PER_UNIT;
522 #endif
523
524 /* The size of the union, based on the fields scanned so far,
525 is max (CONST_SIZE, VAR_SIZE).
526 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
527 register int const_size = 0;
528 register tree var_size = 0;
529
530 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
531 {
532 /* Enums which are local to this class need not be laid out. */
533 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
534 continue;
535
536 layout_decl (field, 0);
537 DECL_FIELD_BITPOS (field) = size_int (0);
538
539 /* Union must be at least as aligned as any field requires. */
540
541 union_align = MAX (union_align, DECL_ALIGN (field));
542
543 #ifdef PCC_BITFIELD_TYPE_MATTERS
544 /* On the m88000, a bit field of declare type `int'
545 forces the entire union to have `int' alignment. */
546 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
547 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
548 #endif
549
550 /* Set union_size to max (decl_size, union_size).
551 There are more and less general ways to do this.
552 Use only CONST_SIZE unless forced to use VAR_SIZE. */
553
554 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
555 const_size = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
556 else if (var_size == 0)
557 var_size = DECL_SIZE (field);
558 else
559 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
560 }
561
562 /* Determine the ultimate size of the union (in bytes). */
563 if (NULL == var_size)
564 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
565 * BITS_PER_UNIT);
566 else if (const_size == 0)
567 TYPE_SIZE (rec) = var_size;
568 else
569 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
570 round_up (size_int (const_size),
571 BITS_PER_UNIT));
572
573 /* Determine the desired alignment. */
574 #ifdef ROUND_TYPE_ALIGN
575 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
576 #else
577 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
578 #endif
579
580 #ifdef ROUND_TYPE_SIZE
581 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
582 #else
583 /* Round the size up to be a multiple of the required alignment */
584 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
585 #endif
586 }
587 \f
588 /* Calculate the mode, size, and alignment for TYPE.
589 For an array type, calculate the element separation as well.
590 Record TYPE on the chain of permanent or temporary types
591 so that dbxout will find out about it.
592
593 TYPE_SIZE of a type is nonzero if the type has been laid out already.
594 layout_type does nothing on such a type.
595
596 If the type is incomplete, its TYPE_SIZE remains zero. */
597
598 void
599 layout_type (type)
600 tree type;
601 {
602 int old;
603 tree pending_statics;
604
605 if (type == 0)
606 abort ();
607
608 /* Do nothing if type has been laid out before. */
609 if (TYPE_SIZE (type))
610 return;
611
612 /* Make sure all nodes we allocate are not momentary;
613 they must last past the current statement. */
614 old = suspend_momentary ();
615
616 /* If we are processing a permanent type, make nodes permanent.
617 If processing a temporary type, make it saveable, since the
618 type node itself is. This is important if the function is inline,
619 since its decls will get copied later. */
620 push_obstacks_nochange ();
621 if (allocation_temporary_p ())
622 {
623 if (TREE_PERMANENT (type))
624 end_temporary_allocation ();
625 else
626 saveable_allocation ();
627 }
628
629 switch (TREE_CODE (type))
630 {
631 case LANG_TYPE:
632 /* This kind of type is the responsibility
633 of the languge-specific code. */
634 abort ();
635
636 case INTEGER_TYPE:
637 case ENUMERAL_TYPE:
638 if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
639 TREE_UNSIGNED (type) = 1;
640
641 /* We pass 0 for the last arg of mode_for_size because otherwise
642 on the Apollo using long long causes a crash.
643 It seems better to use integer modes than to try to support
644 integer types with BLKmode. */
645 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_INT, 0);
646 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
647 break;
648
649 case REAL_TYPE:
650 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
651 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
652 break;
653
654 case COMPLEX_TYPE:
655 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
656 TYPE_MODE (type)
657 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
658 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
659 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
660 0);
661 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
662 break;
663
664 case VOID_TYPE:
665 TYPE_SIZE (type) = size_zero_node;
666 TYPE_ALIGN (type) = 1;
667 TYPE_MODE (type) = VOIDmode;
668 break;
669
670 case OFFSET_TYPE:
671 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (Pmode));
672 TYPE_MODE (type) = Pmode;
673 break;
674
675 case FUNCTION_TYPE:
676 case METHOD_TYPE:
677 TYPE_MODE (type) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode),
678 MODE_INT, 0);
679 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
680 break;
681
682 case POINTER_TYPE:
683 case REFERENCE_TYPE:
684 TYPE_MODE (type) = Pmode;
685 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
686 TREE_UNSIGNED (type) = 1;
687 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
688 break;
689
690 case ARRAY_TYPE:
691 {
692 register tree index = TYPE_DOMAIN (type);
693 register tree element = TREE_TYPE (type);
694
695 build_pointer_type (element);
696
697 /* We need to know both bounds in order to compute the size. */
698 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
699 && TYPE_SIZE (element))
700 {
701 tree length
702 = size_binop (PLUS_EXPR, size_one_node,
703 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
704 TYPE_MIN_VALUE (index)));
705
706 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
707 TYPE_SIZE (element));
708 }
709
710 /* Now round the alignment and size,
711 using machine-dependent criteria if any. */
712
713 #ifdef ROUND_TYPE_ALIGN
714 TYPE_ALIGN (type)
715 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
716 #else
717 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
718 #endif
719
720 #ifdef ROUND_TYPE_SIZE
721 if (TYPE_SIZE (type) != 0)
722 TYPE_SIZE (type)
723 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
724 #endif
725
726 TYPE_MODE (type) = BLKmode;
727 if (TYPE_SIZE (type) != 0
728 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
729 /* BLKmode elements force BLKmode aggregate;
730 else extract/store fields may lose. */
731 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
732 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
733 {
734 TYPE_MODE (type)
735 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
736 MODE_INT, 1);
737
738 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
739 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
740 && TYPE_MODE (type) != BLKmode)
741 {
742 TYPE_NO_FORCE_BLK (type) = 1;
743 TYPE_MODE (type) = BLKmode;
744 }
745 }
746 break;
747 }
748
749 case RECORD_TYPE:
750 pending_statics = layout_record (type);
751 TYPE_MODE (type) = BLKmode;
752 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
753 {
754 tree field;
755 /* A record which has any BLKmode members must itself be BLKmode;
756 it can't go in a register.
757 Unless the member is BLKmode only because it isn't aligned. */
758 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
759 {
760 int bitpos;
761
762 if (TREE_CODE (field) != FIELD_DECL)
763 continue;
764
765 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
766 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
767 goto record_lose;
768
769 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
770 goto record_lose;
771
772 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
773
774 /* Must be BLKmode if any field crosses a word boundary,
775 since extract_bit_field can't handle that in registers. */
776 if (bitpos / BITS_PER_WORD
777 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
778 / BITS_PER_WORD)
779 /* But there is no problem if the field is entire words. */
780 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
781 goto record_lose;
782 }
783
784 TYPE_MODE (type)
785 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
786 MODE_INT, 1);
787
788 /* If structure's known alignment is less than
789 what the scalar mode would need, and it matters,
790 then stick with BLKmode. */
791 if (STRICT_ALIGNMENT
792 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
793 || (TYPE_ALIGN (type)
794 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
795 {
796 if (TYPE_MODE (type) != BLKmode)
797 /* If this is the only reason this type is BLKmode,
798 then don't force containing types to be BLKmode. */
799 TYPE_NO_FORCE_BLK (type) = 1;
800 TYPE_MODE (type) = BLKmode;
801 }
802
803 record_lose: ;
804 }
805
806 /* Lay out any static members. This is done now
807 because their type may use the record's type. */
808 while (pending_statics)
809 {
810 layout_decl (TREE_VALUE (pending_statics), 0);
811 pending_statics = TREE_CHAIN (pending_statics);
812 }
813 break;
814
815 case UNION_TYPE:
816 layout_union (type);
817 TYPE_MODE (type) = BLKmode;
818 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
819 /* If structure's known alignment is less than
820 what the scalar mode would need, and it matters,
821 then stick with BLKmode. */
822 && (! STRICT_ALIGNMENT
823 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
824 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
825 {
826 tree field;
827 /* A union which has any BLKmode members must itself be BLKmode;
828 it can't go in a register.
829 Unless the member is BLKmode only because it isn't aligned. */
830 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
831 {
832 if (TREE_CODE (field) != FIELD_DECL)
833 continue;
834
835 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
836 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
837 goto union_lose;
838 }
839
840 TYPE_MODE (type)
841 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
842 MODE_INT, 1);
843
844 union_lose: ;
845 }
846 break;
847
848 /* Pascal types */
849 case BOOLEAN_TYPE: /* store one byte/boolean for now. */
850 TYPE_MODE (type) = QImode;
851 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
852 TYPE_PRECISION (type) = 1;
853 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
854 break;
855
856 case CHAR_TYPE:
857 TYPE_MODE (type) = QImode;
858 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
859 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
860 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
861 break;
862
863 case FILE_TYPE:
864 /* The size may vary in different languages, so the language front end
865 should fill in the size. */
866 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
867 TYPE_MODE (type) = BLKmode;
868 break;
869
870 default:
871 abort ();
872 } /* end switch */
873
874 /* Normally, use the alignment corresponding to the mode chosen.
875 However, where strict alignment is not required, avoid
876 over-aligning structures, since most compilers do not do this
877 alignment. */
878
879 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
880 && (STRICT_ALIGNMENT
881 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
882 && TREE_CODE (type) != ARRAY_TYPE)))
883 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
884
885 /* Evaluate nonconstant size only once, either now or as soon as safe. */
886 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
887 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
888
889 /* Also layout any other variants of the type. */
890 if (TYPE_NEXT_VARIANT (type)
891 || type != TYPE_MAIN_VARIANT (type))
892 {
893 tree variant;
894 /* Record layout info of this variant. */
895 tree size = TYPE_SIZE (type);
896 int align = TYPE_ALIGN (type);
897 enum machine_mode mode = TYPE_MODE (type);
898
899 /* Copy it into all variants. */
900 for (variant = TYPE_MAIN_VARIANT (type);
901 variant;
902 variant = TYPE_NEXT_VARIANT (variant))
903 {
904 TYPE_SIZE (variant) = size;
905 TYPE_ALIGN (variant) = align;
906 TYPE_MODE (variant) = mode;
907 }
908 }
909
910 pop_obstacks ();
911 resume_momentary (old);
912 }
913 \f
914 /* Create and return a type for signed integers of PRECISION bits. */
915
916 tree
917 make_signed_type (precision)
918 int precision;
919 {
920 register tree type = make_node (INTEGER_TYPE);
921
922 TYPE_PRECISION (type) = precision;
923
924 /* Create the extreme values based on the number of bits. */
925
926 TYPE_MIN_VALUE (type)
927 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
928 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
929 (((HOST_WIDE_INT) (-1)
930 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
931 ? precision - HOST_BITS_PER_WIDE_INT - 1
932 : 0))));
933 TYPE_MAX_VALUE (type)
934 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
935 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
936 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
937 ? (((HOST_WIDE_INT) 1
938 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
939 : 0));
940
941 /* Give this type's extreme values this type as their type. */
942
943 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
944 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
945
946 /* The first type made with this or `make_unsigned_type'
947 is the type for size values. */
948
949 if (sizetype == 0)
950 {
951 sizetype = type;
952 }
953
954 /* Lay out the type: set its alignment, size, etc. */
955
956 layout_type (type);
957
958 return type;
959 }
960
961 /* Create and return a type for unsigned integers of PRECISION bits. */
962
963 tree
964 make_unsigned_type (precision)
965 int precision;
966 {
967 register tree type = make_node (INTEGER_TYPE);
968
969 TYPE_PRECISION (type) = precision;
970
971 /* The first type made with this or `make_signed_type'
972 is the type for size values. */
973
974 if (sizetype == 0)
975 {
976 sizetype = type;
977 }
978
979 fixup_unsigned_type (type);
980 return type;
981 }
982
983 /* Set the extreme values of TYPE based on its precision in bits,
984 then lay it out. Used when make_signed_type won't do
985 because the tree code is not INTEGER_TYPE.
986 E.g. for Pascal, when the -fsigned-char option is given. */
987
988 void
989 fixup_signed_type (type)
990 tree type;
991 {
992 register int precision = TYPE_PRECISION (type);
993
994 TYPE_MIN_VALUE (type)
995 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
996 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
997 (((HOST_WIDE_INT) (-1)
998 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
999 ? precision - HOST_BITS_PER_WIDE_INT - 1
1000 : 0))));
1001 TYPE_MAX_VALUE (type)
1002 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1003 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1004 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1005 ? (((HOST_WIDE_INT) 1
1006 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1007 : 0));
1008
1009 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1010 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1011
1012 /* Lay out the type: set its alignment, size, etc. */
1013
1014 layout_type (type);
1015 }
1016
1017 /* Set the extreme values of TYPE based on its precision in bits,
1018 then lay it out. This is used both in `make_unsigned_type'
1019 and for enumeral types. */
1020
1021 void
1022 fixup_unsigned_type (type)
1023 tree type;
1024 {
1025 register int precision = TYPE_PRECISION (type);
1026
1027 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1028 TYPE_MAX_VALUE (type)
1029 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1030 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1031 precision - HOST_BITS_PER_WIDE_INT > 0
1032 ? ((unsigned HOST_WIDE_INT) ~0
1033 >> (HOST_BITS_PER_WIDE_INT
1034 - (precision - HOST_BITS_PER_WIDE_INT)))
1035 : 0);
1036 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1037 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1038
1039 /* Lay out the type: set its alignment, size, etc. */
1040
1041 layout_type (type);
1042 }
1043 \f
1044 /* Find the best machine mode to use when referencing a bit field of length
1045 BITSIZE bits starting at BITPOS.
1046
1047 The underlying object is known to be aligned to a boundary of ALIGN bits.
1048 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1049 larger than LARGEST_MODE (usually SImode).
1050
1051 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1052 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1053 mode meeting these conditions.
1054
1055 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1056 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1057 all the conditions. */
1058
1059 enum machine_mode
1060 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1061 int bitsize, bitpos;
1062 int align;
1063 enum machine_mode largest_mode;
1064 int volatilep;
1065 {
1066 enum machine_mode mode;
1067 int unit;
1068
1069 /* Find the narrowest integer mode that contains the bit field. */
1070 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1071 mode = GET_MODE_WIDER_MODE (mode))
1072 {
1073 unit = GET_MODE_BITSIZE (mode);
1074 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1075 break;
1076 }
1077
1078 if (mode == MAX_MACHINE_MODE
1079 /* It is tempting to omit the following line
1080 if STRICT_ALIGNMENT is true.
1081 But that is incorrect, since if the bitfield uses part of 3 bytes
1082 and we use a 4-byte mode, we could get a spurious segv
1083 if the extra 4th byte is past the end of memory.
1084 (Though at least one Unix compiler ignores this problem:
1085 that on the Sequent 386 machine. */
1086 || MIN (unit, BIGGEST_ALIGNMENT) > align
1087 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1088 return VOIDmode;
1089
1090 if (SLOW_BYTE_ACCESS && ! volatilep)
1091 {
1092 enum machine_mode wide_mode = VOIDmode, tmode;
1093
1094 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1095 tmode = GET_MODE_WIDER_MODE (tmode))
1096 {
1097 unit = GET_MODE_BITSIZE (tmode);
1098 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1099 && unit <= BITS_PER_WORD
1100 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1101 && (largest_mode == VOIDmode
1102 || unit <= GET_MODE_BITSIZE (largest_mode)))
1103 wide_mode = tmode;
1104 }
1105
1106 if (wide_mode != VOIDmode)
1107 return wide_mode;
1108 }
1109
1110 return mode;
1111 }
1112 \f
1113 /* Save all variables describing the current status into the structure *P.
1114 This is used before starting a nested function. */
1115
1116 void
1117 save_storage_status (p)
1118 struct function *p;
1119 {
1120 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1121 p->pending_sizes = pending_sizes;
1122 p->immediate_size_expand = immediate_size_expand;
1123 #endif /* 0 */
1124 }
1125
1126 /* Restore all variables describing the current status from the structure *P.
1127 This is used after a nested function. */
1128
1129 void
1130 restore_storage_status (p)
1131 struct function *p;
1132 {
1133 #if 0
1134 pending_sizes = p->pending_sizes;
1135 immediate_size_expand = p->immediate_size_expand;
1136 #endif /* 0 */
1137 }
This page took 0.089119 seconds and 5 git commands to generate.