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