No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / g++ / cc1plus / stor-layout.c
blob6affca5cb86160acd4215948f8e303537b4bc64b
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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 1, or (at your option)
9 any later version.
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.
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. */
21 #include "config.h"
22 #include <stdio.h>
24 #include "tree.h"
25 #include "rtl.h" /* For GET_MODE_SIZE */
27 #define MAX(x,y) ((x) > (y) ? (x) : (y))
28 #define MIN(x,y) ((x) < (y) ? (x) : (y))
29 #define CEIL(x,y) (((x) + (y) - 1) / (y))
31 /* Data type for the expressions representing sizes of data types.
32 It is the first integer type laid out.
33 In C, this is int. */
35 tree sizetype;
37 /* An integer constant with value 0 whose type is sizetype. */
39 tree size_zero_node;
41 /* An integer constant with value 1 whose type is sizetype. */
43 tree size_one_node;
45 /* Integer constants with sizes for pointers and reference types,
46 function types, and method types respectively. */
47 static tree ptr_size_node, function_size_node, method_size_node;
49 #define GET_MODE_ALIGNMENT(MODE) \
50 MIN (BIGGEST_ALIGNMENT, \
51 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
53 /* Chain of all permanent types we have allocated since last
54 call to get_permanent_types. */
56 tree permanent_type_chain;
58 /* Chain of all temporary types we have allocated in this function. */
60 tree temporary_type_chain;
62 /* When the chains is not null, these point at the last
63 types on the two chains. These help us tell whether a type
64 is already on a chain. */
65 tree permanent_type_end;
66 tree temporary_type_end;
68 /* Put the newly-made type T
69 on either permanent_type_chain or temporary_type_chain.
70 Types that are const or volatile variants of other types
71 are not put on any chain, since in the gdb symbol segment
72 we do not make those distinctions.
74 If T is already on the chain, we do nothing. */
76 void
77 chain_type (t)
78 tree t;
80 if (TYPE_MAIN_VARIANT (t) != t)
81 return;
82 if (TREE_CHAIN (t) != 0)
83 return;
84 if (TREE_PERMANENT (t))
86 /* If T is on the chain at the end, don't chain it to itself! */
87 if (t == permanent_type_end)
88 return;
89 /* Add T to the end of the chain. */
90 if (permanent_type_chain == 0)
91 permanent_type_chain = t;
92 else
93 TREE_CHAIN (permanent_type_end) = t;
94 permanent_type_end = t;
96 else
98 if (t == temporary_type_end)
99 return;
100 if (temporary_type_chain == 0)
101 temporary_type_chain = t;
102 else
103 TREE_CHAIN (temporary_type_end) = t;
104 temporary_type_end = t;
108 /* Get a chain of all permanent types made since this function
109 was last called. */
111 tree
112 get_permanent_types ()
114 register tree tem = permanent_type_chain;
115 permanent_type_chain = 0;
116 permanent_type_end = 0;
117 return tem;
120 /* Get a chain of all temporary types made since this function
121 was last called. */
123 tree
124 get_temporary_types ()
126 register tree tem = temporary_type_chain;
127 temporary_type_chain = 0;
128 temporary_type_end = 0;
129 return tem;
132 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
134 static tree pending_sizes;
136 /* Nonzero means cannot safely call expand_expr now,
137 so put variable sizes onto `pending_sizes' instead. */
139 int immediate_size_expand;
141 tree
142 get_pending_sizes ()
144 tree chain = pending_sizes;
145 pending_sizes = 0;
146 return chain;
149 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
150 to serve as the actual size-expression for a type or decl. */
152 static tree
153 variable_size (size)
154 tree size;
156 size = save_expr (size);
158 if (global_bindings_p ())
160 error ("variable-size type declared outside of any function");
161 return build_int (1);
164 if (immediate_size_expand)
165 expand_expr (size, 0, VOIDmode, 0);
166 else
167 pending_sizes = tree_cons (0, size, pending_sizes);
169 return size;
172 /* Return the machine mode to use for an aggregate of SIZE bits.
174 Note!!! We only use a non-BLKmode mode if the size matches exactly.
175 There used to be the idea of using DImode for anything whose
176 size was less than DImode but more than SImode. This does not work
177 because DImode moves cannot be used to store such objects in memory. */
179 #ifndef MAX_FIXED_MODE_SIZE
180 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
181 #endif
183 static
184 enum machine_mode
185 agg_mode (size)
186 unsigned int size;
188 register int units = size / BITS_PER_UNIT;
189 register enum machine_mode t, val;
191 if (size % BITS_PER_UNIT != 0)
192 return BLKmode;
194 if (size > MAX_FIXED_MODE_SIZE)
195 return BLKmode;
197 /* Get the last mode which has this size. */
198 val = BLKmode;
199 for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
200 t = (enum machine_mode) ((int) t + 1))
201 if (GET_MODE_SIZE (t) == units)
202 val = t;
204 return val;
207 /* Return an INTEGER_CST with value V and type from `sizetype'. */
209 tree
210 build_int (v)
211 int v;
213 register tree t;
215 t = build_int_2 (v, 0);
216 TREE_TYPE (t) = sizetype;
217 return t;
220 /* Combine operands OP1 and OP2 with arithmetic operation OPC.
221 OPC is a tree code. Data type is taken from `sizetype',
222 If the operands are constant, so is the result. */
224 tree
225 genop (opc, op1, op2)
226 enum tree_code opc;
227 tree op1, op2;
229 /* Handle the special case of two integer constants faster. */
230 if (TREE_CODE (op1) == INTEGER_CST && TREE_CODE (op2) == INTEGER_CST)
232 /* And some specific cases even faster than that. */
233 if (opc == PLUS_EXPR
234 && TREE_INT_CST_LOW (op1) == 0
235 && TREE_INT_CST_HIGH (op1) == 0)
236 return op2;
237 if (opc == MINUS_EXPR
238 && TREE_INT_CST_LOW (op2) == 0
239 && TREE_INT_CST_HIGH (op2) == 0)
240 return op1;
241 if (opc == MULT_EXPR
242 && TREE_INT_CST_LOW (op1) == 1
243 && TREE_INT_CST_HIGH (op1) == 0)
244 return op2;
245 if (opc == CEIL_DIV_EXPR
246 && TREE_INT_CST_LOW (op1) == TREE_INT_CST_LOW (op2)
247 && TREE_INT_CST_HIGH (op1) == TREE_INT_CST_HIGH (op2))
248 return size_one_node;
249 /* Handle general case of two integer constants. */
250 return combine (opc, op1, op2);
253 if (op1 == error_mark_node || op2 == error_mark_node)
254 return error_mark_node;
256 return fold (build (opc, sizetype, op1, op2));
259 /* Convert a size which is SIZE when expressed in unit INUNITS
260 into the units OUTUNITS. Rounds up if conversion is not exact.
261 If SIZE is constant, so is the result. */
263 tree
264 convert_units (size, inunits, outunits)
265 tree size;
266 register int inunits, outunits;
268 register tree t;
270 if (inunits == outunits)
271 return size;
272 /* Check for inunits divisible by outunits.
273 In that case, just multiply by their ratio. */
274 if (0 == (inunits % outunits))
275 return genop (MULT_EXPR, size, build_int (inunits / outunits));
276 /* The inverse case. */
277 if (0 == (outunits % inunits))
279 /* Discard anything in SIZE to round it up to a multiple
280 of a number N that divides our current divisor. */
281 if (TREE_CODE (size) == MULT_EXPR
282 && TREE_CODE (TREE_OPERAND (size, 1)) == INTEGER_CST
283 && 0 == (outunits / inunits) % TREE_INT_CST_LOW (TREE_OPERAND (size, 1))
284 && TREE_CODE (TREE_OPERAND (size, 0)) == CEIL_DIV_EXPR
285 && tree_int_cst_equal (TREE_OPERAND (size, 1),
286 TREE_OPERAND (TREE_OPERAND (size, 0), 1)))
287 size = TREE_OPERAND (TREE_OPERAND (size, 0), 0);
288 return genop (CEIL_DIV_EXPR, size, build_int (outunits / inunits));
290 /* The general case. */
291 t = genop (MULT_EXPR, size,
292 build_int (inunits)); /* convert to bits */
293 return genop (CEIL_DIV_EXPR, t,
294 build_int (outunits)); /* then to outunits */
297 /* Set the size, mode and alignment of a ..._DECL node.
298 TYPE_DECL does need this for C++. It is up to language-specific
299 code to intialize the DECL_OFFSET of TYPE_DECL nodes.
300 Note that LABEL_DECL and CONST_DECL nodes do not need this,
301 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
302 Don't call layout_decl for them.
304 KNOWN_ALIGN is the amount of alignment we can assume this
305 decl has with no special effort. It is relevant only for FIELD_DECLs
306 and depends on the previous fields.
307 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
308 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
309 the record will be aligned to suit. */
311 void
312 layout_decl (decl, known_align)
313 tree decl;
314 unsigned known_align;
316 register tree type = TREE_TYPE (decl);
317 register enum tree_code code = TREE_CODE (decl);
318 int spec_size = DECL_SIZE_UNIT (decl);
319 int bitsize;
321 if (code == CONST_DECL)
322 return;
324 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
325 && code != FIELD_DECL && code != TYPE_DECL)
326 abort ();
328 if (type == error_mark_node)
330 type = void_type_node;
331 spec_size = 0;
333 if (TYPE_SIZE_UNIT (type) == 0)
334 abort ();
336 /* Usually the size and mode come from the data type without change. */
338 DECL_MODE (decl) = TYPE_MODE (type);
339 DECL_SIZE (decl) = TYPE_SIZE (type);
340 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
341 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
343 if (code == FIELD_DECL && TREE_PACKED (decl))
345 /* This is a bit-field. We don't know how to handle
346 them except for integers and enums, and front end should
347 never generate them otherwise. */
349 if (! (TREE_CODE (type) == INTEGER_TYPE
350 || TREE_CODE (type) == ENUMERAL_TYPE))
351 abort ();
353 if (spec_size == 0)
354 abort ();
356 /* Mode is "integer bit field". */
357 DECL_MODE (decl) = BImode;
358 /* Size is specified number of bits. */
359 DECL_SIZE (decl) = size_one_node;
360 DECL_SIZE_UNIT (decl) = spec_size;
362 /* Force alignment required for the data type.
363 But if the decl itself wants greater alignment, don't override that. */
364 else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
365 DECL_ALIGN (decl) = TYPE_ALIGN (type);
367 if (DECL_SIZE (decl))
368 bitsize = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
370 /* See if we can use a scalar mode such as QImode or SImode
371 in place of BLKmode or a packed byte mode. */
372 /* Conditions are: a fixed size that is correct for another mode
373 and occupying a complete byte or bytes on proper boundary. */
374 if ((DECL_MODE (decl) == BLKmode
375 || DECL_MODE (decl) == BImode)
376 /* Don't do this if DECL's type requires it to be BLKmode. */
377 && TYPE_MODE (type) != BLKmode
378 && TYPE_SIZE (type) != 0
379 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
381 register enum machine_mode xmode = agg_mode (bitsize);
383 if (xmode != BLKmode
384 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
386 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
387 DECL_ALIGN (decl));
388 DECL_MODE (decl) = xmode;
389 DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
390 DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
391 bitsize = GET_MODE_BITSIZE (xmode);
395 /* Don't let more than one word of an aggregate occupy one register,
396 since then the SUBREG used to access the high part would malfunction.
397 Check that the expected # of registers is big enough that they
398 seem to hold this variable with just a word per register. */
399 if (DECL_SIZE (decl) != 0
400 && (TREE_CODE (type) == RECORD_TYPE
401 || TREE_CODE (type) == UNION_TYPE
402 || TREE_CODE (type) == ARRAY_TYPE))
404 /* This test is not exactly right, since we really want the minimum
405 number of regs in any class that can hold this mode.
406 But it does distinguish the machines we need to distinguish,
407 for now. */
408 if (CLASS_MAX_NREGS (ALL_REGS, TYPE_MODE (type)) * BITS_PER_WORD
409 < bitsize)
410 TREE_ADDRESSABLE (decl) = 1;
413 /* Evaluate nonconstant size only once, either now or as soon as safe. */
414 if (DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
415 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
418 /* Lay out a RECORD_TYPE type (a C struct).
419 This means laying out the fields, determining their offsets,
420 and computing the overall size and required alignment of the record.
421 Note that if you set the TYPE_ALIGN before calling this
422 then the struct is aligned to at least that boundary.
424 If the type has basetypes, you must call layout_basetypes
425 before calling this function. */
427 static void
428 layout_record (rec)
429 tree rec;
431 register tree field;
432 #ifdef STRUCTURE_SIZE_BOUNDARY
433 int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
434 #else
435 int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
436 #endif
437 /* These must be laid out *after* the record is. */
438 tree pending_statics = NULL_TREE;
439 /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
440 where CONST_SIZE is an integer
441 and VAR_SIZE is a tree expression.
442 If VAR_SIZE is null, the size is just CONST_SIZE.
443 Naturally we try to avoid using VAR_SIZE. */
444 register int const_size = 0;
445 register tree var_size = 0;
446 register int size_unit = BITS_PER_UNIT;
448 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
450 register int desired_align;
452 /* If FIELD is a VAR_DECL, then treat it like a separate variable,
453 not really like a structure field.
454 If it is a FUNCTION_DECL, it's a method.
455 In both cases, all we do is lay out the decl,
456 and we do it *after* the record is laid out. */
458 if (TREE_STATIC (field))
460 pending_statics = tree_cons (NULL, field, pending_statics);
461 continue;
463 /* Enumerators and enum types which are local to this class need not
464 be laid out. Same with initialized constant fields. */
465 if (TREE_CODE (field) != FIELD_DECL)
466 continue;
468 /* Lay out the field so we know what alignment it needs.
469 For KNOWN_ALIGN, pass the number of bits from start of record
470 or some divisor of it. */
472 layout_decl (field, var_size ? size_unit : const_size);
473 desired_align = DECL_ALIGN (field);
475 /* Record must have at least as much alignment as any field.
476 Otherwise, the alignment of the field within the record
477 is meaningless. */
479 record_align = MAX (record_align, desired_align);
480 #ifdef PCC_BITFIELD_TYPE_MATTERS
481 /* In PCC on Vax, Sony, etc., a bit field of declare type `int'
482 forces the entire structure to have `int' alignment. */
483 if (DECL_NAME (field) != 0)
484 record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
485 #endif
487 /* Does this field automatically have alignment it needs
488 by virtue of the fields that precede it and the record's
489 own alignment? */
491 if (const_size % desired_align != 0
492 || (size_unit % desired_align != 0
493 && var_size))
495 /* No, we need to skip space before this field.
496 Bump the cumulative size to multiple of field alignment. */
498 if (var_size == 0
499 || size_unit % desired_align == 0)
500 const_size
501 = CEIL (const_size, desired_align) * desired_align;
502 else
504 var_size
505 = genop (PLUS_EXPR, var_size,
506 build_int (CEIL (const_size, size_unit)));
507 const_size = 0;
508 var_size = convert_units (var_size, size_unit, desired_align);
509 size_unit = desired_align;
513 #ifdef PCC_BITFIELD_TYPE_MATTERS
514 if (TREE_CODE (field) == FIELD_DECL
515 && TREE_TYPE (field) != error_mark_node)
517 int type_align = TYPE_ALIGN (TREE_TYPE (field));
518 register tree dsize = DECL_SIZE (field);
519 int field_size = TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
521 /* A bit field may not span the unit of alignment of its type.
522 Advance to next boundary if necessary. */
523 if (const_size / type_align
524 != (const_size + field_size - 1) / type_align)
525 const_size = CEIL (const_size, type_align) * type_align;
527 #endif
529 /* Size so far becomes the offset of this field. */
531 DECL_OFFSET (field) = const_size;
532 DECL_VOFFSET (field) = var_size;
533 DECL_VOFFSET_UNIT (field) = size_unit;
535 /* If this field is an anonymous union,
536 give each union-member the same offset as the union has. */
538 if (DECL_NAME (field) == 0
539 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
541 tree uelt = TYPE_FIELDS (TREE_TYPE (field));
542 for (; uelt; uelt = TREE_CHAIN (uelt))
544 DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
545 DECL_OFFSET (uelt) = DECL_OFFSET (field);
546 DECL_VOFFSET (uelt) = DECL_VOFFSET (field);
547 DECL_VOFFSET_UNIT (uelt) = DECL_VOFFSET_UNIT (field);
551 /* Now add size of this field to the size of the record. */
554 register tree dsize = DECL_SIZE (field);
556 if (TREE_LITERAL (dsize))
557 const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
558 else if (var_size == 0)
560 var_size = dsize;
561 size_unit = DECL_SIZE_UNIT (field);
563 else
565 register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
566 var_size
567 = genop (PLUS_EXPR,
568 convert_units (var_size, size_unit, tunits),
569 convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
574 /* Work out the total size and alignment of the record
575 as one expression and store in the record type.
576 Round it up to a multiple of the record's alignment. */
578 if (var_size == 0)
579 TYPE_SIZE (rec)
580 = build_int (CEIL (CEIL (const_size, record_align) * record_align,
581 size_unit));
582 else
584 if (const_size)
585 var_size
586 = genop (PLUS_EXPR, var_size,
587 build_int (CEIL (const_size, size_unit)));
588 TYPE_SIZE (rec)
589 = convert_units (var_size,
590 size_unit,
591 record_align);
592 size_unit = record_align;
595 TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
596 BITS_PER_UNIT);
597 TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
598 TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
600 /* Lay out any static members. This is done now
601 because their type may use the record's type. */
603 for (field = pending_statics; field; field = TREE_CHAIN (field))
604 layout_decl (TREE_VALUE (field), 0);
607 /* Lay out a UNION_TYPE type.
608 Lay out all the fields, set their offsets to zero,
609 and compute the size and alignment of the union (maximum of any field).
610 Note that if you set the TYPE_ALIGN before calling this
611 then the union align is aligned to at least that boundary. */
613 static void
614 layout_union (rec)
615 tree rec;
617 register tree field;
618 #ifdef STRUCTURE_SIZE_BOUNDARY
619 int union_align = STRUCTURE_SIZE_BOUNDARY;
620 #else
621 int union_align = BITS_PER_UNIT;
622 #endif
624 /* The size of the union, based on the fields scanned so far,
625 is max (CONST_SIZE, VAR_SIZE).
626 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
627 register int const_size = 0;
628 register tree var_size = 0;
630 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
632 /* Enums which are local to this class need not
633 be laid out. */
634 if (TREE_CODE (field) != FIELD_DECL)
635 continue;
637 layout_decl (field, 0);
638 DECL_OFFSET (field) = 0;
639 DECL_VOFFSET (field) = 0;
640 DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
642 /* Union must be at least as aligned as any field requires. */
644 union_align = MAX (union_align, DECL_ALIGN (field));
646 #ifdef PCC_BITFIELD_TYPE_MATTERS
647 /* On the m88000, a bit field of declare type `int'
648 forces the entire union to have `int' alignment. */
649 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
650 #endif
652 /* Set union_size to max (decl_size, union_size).
653 There are more and less general ways to do this.
654 Use only CONST_SIZE unless forced to use VAR_SIZE. */
656 if (TREE_LITERAL (DECL_SIZE (field)))
657 const_size = MAX (const_size,
658 TREE_INT_CST_LOW (DECL_SIZE (field))
659 * DECL_SIZE_UNIT (field));
660 else if (var_size == 0)
661 var_size = convert_units (DECL_SIZE (field),
662 DECL_SIZE_UNIT (field),
663 BITS_PER_UNIT);
664 else
665 var_size = genop (MAX_EXPR,
666 convert_units (DECL_SIZE (field),
667 DECL_SIZE_UNIT (field),
668 BITS_PER_UNIT),
669 var_size);
672 /* Determine the ultimate size of the union (in bytes). */
673 if (NULL == var_size)
674 TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
675 else if (const_size == 0)
676 TYPE_SIZE (rec) = var_size;
677 else
678 TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
679 build_int (CEIL (const_size, BITS_PER_UNIT)));
681 /* Determine the desired alignment. */
682 union_align = MIN (BIGGEST_ALIGNMENT, union_align);
683 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
685 /* Round the size up to be a multiple of the required alignment */
686 TYPE_SIZE (rec)
687 = convert_units (TYPE_SIZE (rec), BITS_PER_UNIT, TYPE_ALIGN (rec));
688 TYPE_SIZE_UNIT (rec) = TYPE_ALIGN (rec);
691 /* Calculate the mode, size, and alignment for TYPE.
692 For an array type, calculate the element separation as well.
693 Record TYPE on the chain of permanent or temporary types
694 so that dbxout will find out about it.
696 TYPE_SIZE of a type is nonzero if the type has been laid out already.
697 layout_type does nothing on such a type.
699 If the type is incomplete, its TYPE_SIZE remains zero. */
701 void
702 layout_type (type)
703 tree type;
705 int old;
706 int temporary = 0;
708 if (type == 0)
709 abort ();
711 /* Do nothing if type has been laid out before. */
712 if (TYPE_SIZE (type))
713 return;
715 /* Make sure all nodes we allocate are not momentary;
716 they must last past the current statement. */
717 old = suspend_momentary ();
718 if (TREE_PERMANENT (type) && allocation_temporary_p ())
720 temporary = 1;
721 end_temporary_allocation ();
724 chain_type (type);
726 switch (TREE_CODE (type))
728 case LANG_TYPE:
729 /* This kind of type is the responsibility
730 of the languge-specific code. */
731 abort ();
733 case VOID_TYPE:
734 TYPE_SIZE (type) = size_zero_node;
735 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
736 TYPE_ALIGN (type) = 1;
737 TYPE_MODE (type) = VOIDmode;
738 break;
740 case INTEGER_TYPE:
741 case ENUMERAL_TYPE:
742 if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
743 TREE_UNSIGNED (type) = 1;
745 /* What follows is like agg_mode except that it ignores
746 MAX_FIXED_MODE_SIZE. That applies only to structures. */
748 enum machine_mode mode, t;
750 /* Get the last mode which has this size. */
751 mode = BLKmode;
752 for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
753 t = (enum machine_mode) ((int) t + 1))
754 if (GET_MODE_BITSIZE (t) == TYPE_PRECISION (type))
755 mode = t;
757 TYPE_MODE (type) = mode;
759 TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
760 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
761 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
762 break;
764 case REAL_TYPE:
766 register int prec = TYPE_PRECISION (type);
767 if (prec <= GET_MODE_BITSIZE (SFmode))
768 TYPE_MODE (type) = SFmode;
769 else if (prec <= GET_MODE_BITSIZE (DFmode))
770 TYPE_MODE (type) = DFmode;
771 else if (prec <= GET_MODE_BITSIZE (TFmode))
772 TYPE_MODE (type) = TFmode;
773 else
774 abort ();
776 TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
777 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
778 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
779 break;
781 case POINTER_TYPE:
782 case REFERENCE_TYPE:
783 TYPE_MODE (type) = Pmode;
784 TYPE_SIZE (type) = ptr_size_node;
785 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
786 TYPE_ALIGN (type) = POINTER_BOUNDARY;
787 TREE_UNSIGNED (type) = 1;
788 TYPE_PRECISION (type) = POINTER_SIZE;
789 break;
791 case ARRAY_TYPE:
793 register tree index = TYPE_DOMAIN (type);
794 register tree length;
795 register tree element = TREE_TYPE (type);
797 /* layout_type (element); */
798 build_pointer_type (element);
800 if (index == 0)
801 length = 0;
802 else
803 length = genop (PLUS_EXPR, size_one_node,
804 genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
805 TYPE_MIN_VALUE (index)));
807 if (TREE_PACKED (type))
808 abort (); /* ??? Not written yet since not needed for C. */
810 TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
811 if (length && TYPE_SIZE (element))
812 TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
813 TYPE_SEP (type) = TYPE_SIZE (element);
814 TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
815 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
816 TYPE_MODE (type) = BLKmode;
817 if (TYPE_SIZE (type) != 0
818 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
819 /* BLKmode elements force BLKmode aggregate;
820 else extract/store fields may lose. */
821 && TYPE_MODE (TREE_TYPE (type)) != BLKmode
822 #ifdef STRICT_ALIGNMENT
823 && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
824 || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
825 * TYPE_SIZE_UNIT (type)))
826 #endif
829 TYPE_MODE (type)
830 = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
831 * TYPE_SIZE_UNIT (type));
833 break;
836 case RECORD_TYPE:
837 layout_record (type);
838 TYPE_MODE (type) = BLKmode;
839 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
840 /* If structure's known alignment is less than
841 what the scalar mode would need, and it matters,
842 then stick with BLKmode. */
843 #ifdef STRICT_ALIGNMENT
844 && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
845 || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
846 * TYPE_SIZE_UNIT (type)))
847 #endif
850 tree field;
851 /* A record which has any BLKmode members must itself be BLKmode;
852 it can't go in a register. */
853 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
855 if (TREE_CODE (field) != FIELD_DECL)
856 continue;
858 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
859 goto record_lose;
861 /* Must be BLKmode if any field crosses a word boundary,
862 since extract_bit_field can't handle that in registers. */
863 if (DECL_OFFSET (field) / BITS_PER_WORD
864 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
865 + DECL_OFFSET (field) - 1)
866 / BITS_PER_WORD))
867 goto record_lose;
870 TYPE_MODE (type)
871 = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
872 * TYPE_SIZE_UNIT (type));
873 record_lose: ;
875 break;
877 case UNION_TYPE:
878 layout_union (type);
879 TYPE_MODE (type) = BLKmode;
880 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
881 /* If structure's known alignment is less than
882 what the scalar mode would need, and it matters,
883 then stick with BLKmode. */
884 #ifdef STRICT_ALIGNMENT
885 && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
886 || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
887 * TYPE_SIZE_UNIT (type)))
888 #endif
891 tree field;
892 /* A union which has any BLKmode members must itself be BLKmode;
893 it can't go in a register. */
894 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
895 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
896 goto union_lose;
898 TYPE_MODE (type)
899 = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
900 * TYPE_SIZE_UNIT (type));
901 union_lose: ;
903 break;
905 case FUNCTION_TYPE:
906 TYPE_MODE (type) = EPmode;
907 TYPE_SIZE (type) = function_size_node;
908 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
909 TYPE_ALIGN (type) = POINTER_BOUNDARY;
910 break;
912 case METHOD_TYPE:
913 TYPE_MODE (type) = EPmode;
914 TYPE_SIZE (type) = method_size_node;
915 TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
916 TYPE_ALIGN (type) = POINTER_BOUNDARY;
917 break;
919 default:
920 abort ();
921 } /* end switch */
923 /* Evaluate nonconstant size only once, either now or as soon as safe. */
924 if (TYPE_SIZE (type) != 0 && ! TREE_LITERAL (TYPE_SIZE (type)))
925 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
927 /* Also layout any other variants of the type. */
928 if (TYPE_NEXT_VARIANT (type)
929 || type != TYPE_MAIN_VARIANT (type))
931 tree variant;
932 /* Record layout info of this variant. */
933 tree size = TYPE_SIZE (type);
934 int size_unit = TYPE_SIZE_UNIT (type);
935 int align = TYPE_ALIGN (type);
936 enum machine_mode mode = TYPE_MODE (type);
938 /* Copy it into all variants. */
939 for (variant = TYPE_MAIN_VARIANT (type);
940 variant;
941 variant = TYPE_NEXT_VARIANT (variant))
943 TYPE_SIZE (variant) = size;
944 TYPE_SIZE_UNIT (variant) = size_unit;
945 TYPE_ALIGN (variant) = align;
946 TYPE_MODE (variant) = mode;
950 if (temporary)
951 resume_temporary_allocation ();
952 resume_momentary (old);
955 /* Create and return a type for signed integers of PRECISION bits. */
957 tree
958 make_signed_type (precision)
959 int precision;
961 register tree type = make_node (INTEGER_TYPE);
963 TYPE_PRECISION (type) = precision;
965 /* Create the extreme values based on the number of bits. */
967 TYPE_MIN_VALUE (type)
968 = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
969 (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
970 ? precision-HOST_BITS_PER_INT-1
971 : 0));
972 TYPE_MAX_VALUE (type)
973 = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
974 (precision-HOST_BITS_PER_INT-1 > 0
975 ? (1<<(precision-HOST_BITS_PER_INT-1))-1
976 : 0));
978 /* Give this type's extreme values this type as their type. */
980 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
981 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
983 /* The first type made with this or `make_unsigned_type'
984 is the type for size values. */
986 if (sizetype == 0)
988 sizetype = type;
989 ptr_size_node = build_int (POINTER_SIZE / BITS_PER_UNIT);
990 function_size_node = ptr_size_node;
991 method_size_node = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
994 /* Lay out the type: set its alignment, size, etc. */
996 layout_type (type);
998 return type;
1001 /* Create and return a type for unsigned integers of PRECISION bits. */
1003 tree
1004 make_unsigned_type (precision)
1005 int precision;
1007 register tree type = make_node (INTEGER_TYPE);
1009 TYPE_PRECISION (type) = precision;
1011 /* The first type made with this or `make_unsigned_type'
1012 is the type for size values. */
1014 if (sizetype == 0)
1016 sizetype = type;
1017 ptr_size_node = build_int (POINTER_SIZE / BITS_PER_UNIT);
1018 function_size_node = ptr_size_node;
1019 method_size_node = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
1022 fixup_unsigned_type (type);
1023 return type;
1026 /* Set the extreme values of TYPE based on its precision in bits,
1027 the lay it out. This is used both in `make_unsigned_type'
1028 and for enumeral types. */
1030 void
1031 fixup_unsigned_type (type)
1032 tree type;
1034 register int precision = TYPE_PRECISION (type);
1036 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1037 TYPE_MAX_VALUE (type)
1038 = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
1039 precision-HOST_BITS_PER_INT > 0
1040 ? ((unsigned) ~0
1041 >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
1042 : 0);
1043 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1044 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1046 /* Lay out the type: set its alignment, size, etc. */
1048 layout_type (type);