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