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