]>
Commit | Line | Data |
---|---|---|
76e616db | 1 | /* Utility routines for data type conversion for GNU C. |
f5963e61 | 2 | Copyright (C) 1987, 88, 91-95, 97, 1998 Free Software Foundation, Inc. |
76e616db BK |
3 | |
4 | This file is part of GNU C. | |
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 | |
940d9d63 RK |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
76e616db BK |
20 | |
21 | ||
22 | /* These routines are somewhat language-independent utility function | |
0f41302f | 23 | intended to be called by the language-specific convert () functions. */ |
76e616db BK |
24 | |
25 | #include "config.h" | |
26 | #include "tree.h" | |
27 | #include "flags.h" | |
28 | #include "convert.h" | |
10f0ad3d | 29 | #include "toplev.h" |
76e616db | 30 | |
98c76e3c | 31 | /* Convert EXPR to some pointer or reference type TYPE. |
76e616db | 32 | |
98c76e3c | 33 | EXPR must be pointer, reference, integer, enumeral, or literal zero; |
0f41302f | 34 | in other cases error is called. */ |
76e616db BK |
35 | |
36 | tree | |
37 | convert_to_pointer (type, expr) | |
38 | tree type, expr; | |
39 | { | |
76e616db BK |
40 | if (integer_zerop (expr)) |
41 | { | |
76e616db BK |
42 | expr = build_int_2 (0, 0); |
43 | TREE_TYPE (expr) = type; | |
44 | return expr; | |
45 | } | |
46 | ||
f5963e61 | 47 | switch (TREE_CODE (TREE_TYPE (expr))) |
76e616db | 48 | { |
f5963e61 JL |
49 | case POINTER_TYPE: |
50 | case REFERENCE_TYPE: | |
51 | return build1 (NOP_EXPR, type, expr); | |
52 | ||
53 | case INTEGER_TYPE: | |
54 | case ENUMERAL_TYPE: | |
55 | case BOOLEAN_TYPE: | |
56 | case CHAR_TYPE: | |
57 | if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE) | |
76e616db | 58 | return build1 (CONVERT_EXPR, type, expr); |
76e616db | 59 | |
f5963e61 JL |
60 | return |
61 | convert_to_pointer (type, | |
62 | convert (type_for_size (POINTER_SIZE, 0), expr)); | |
76e616db | 63 | |
f5963e61 JL |
64 | default: |
65 | error ("cannot convert to a pointer type"); | |
66 | return convert_to_pointer (type, integer_zero_node); | |
67 | } | |
76e616db BK |
68 | } |
69 | ||
70 | /* Convert EXPR to some floating-point type TYPE. | |
71 | ||
72 | EXPR must be float, integer, or enumeral; | |
0f41302f | 73 | in other cases error is called. */ |
76e616db BK |
74 | |
75 | tree | |
76 | convert_to_real (type, expr) | |
77 | tree type, expr; | |
78 | { | |
f5963e61 JL |
79 | switch (TREE_CODE (TREE_TYPE (expr))) |
80 | { | |
81 | case REAL_TYPE: | |
82 | return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR, | |
83 | type, expr); | |
84 | ||
85 | case INTEGER_TYPE: | |
86 | case ENUMERAL_TYPE: | |
87 | case BOOLEAN_TYPE: | |
88 | case CHAR_TYPE: | |
89 | return build1 (FLOAT_EXPR, type, expr); | |
90 | ||
91 | case COMPLEX_TYPE: | |
92 | return convert (type, | |
93 | fold (build1 (REALPART_EXPR, | |
94 | TREE_TYPE (TREE_TYPE (expr)), expr))); | |
95 | ||
96 | case POINTER_TYPE: | |
97 | case REFERENCE_TYPE: | |
98 | error ("pointer value used where a floating point value was expected"); | |
99 | return convert_to_real (type, integer_zero_node); | |
100 | ||
101 | default: | |
102 | error ("aggregate value used where a float was expected"); | |
103 | return convert_to_real (type, integer_zero_node); | |
104 | } | |
76e616db BK |
105 | } |
106 | ||
107 | /* Convert EXPR to some integer (or enum) type TYPE. | |
108 | ||
109 | EXPR must be pointer, integer, discrete (enum, char, or bool), or float; | |
110 | in other cases error is called. | |
111 | ||
112 | The result of this is always supposed to be a newly created tree node | |
113 | not in use in any existing structure. */ | |
114 | ||
115 | tree | |
116 | convert_to_integer (type, expr) | |
117 | tree type, expr; | |
118 | { | |
f5963e61 JL |
119 | enum tree_code ex_form = TREE_CODE (expr); |
120 | tree intype = TREE_TYPE (expr); | |
121 | int inprec = TYPE_PRECISION (intype); | |
122 | int outprec = TYPE_PRECISION (type); | |
76e616db | 123 | |
f5963e61 | 124 | switch (TREE_CODE (intype)) |
76e616db | 125 | { |
f5963e61 JL |
126 | case POINTER_TYPE: |
127 | case REFERENCE_TYPE: | |
76e616db BK |
128 | if (integer_zerop (expr)) |
129 | expr = integer_zero_node; | |
130 | else | |
131 | expr = fold (build1 (CONVERT_EXPR, | |
132 | type_for_size (POINTER_SIZE, 0), expr)); | |
76e616db | 133 | |
f5963e61 | 134 | return convert_to_integer (type, expr); |
76e616db | 135 | |
f5963e61 JL |
136 | case INTEGER_TYPE: |
137 | case ENUMERAL_TYPE: | |
138 | case BOOLEAN_TYPE: | |
139 | case CHAR_TYPE: | |
140 | /* If this is a logical operation, which just returns 0 or 1, we can | |
141 | change the type of the expression. For some logical operations, | |
142 | we must also change the types of the operands to maintain type | |
c9529354 | 143 | correctness. */ |
76e616db | 144 | |
c9529354 | 145 | if (TREE_CODE_CLASS (ex_form) == '<') |
76e616db BK |
146 | { |
147 | TREE_TYPE (expr) = type; | |
148 | return expr; | |
149 | } | |
f5963e61 | 150 | |
c9529354 RK |
151 | else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR |
152 | || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR | |
153 | || ex_form == TRUTH_XOR_EXPR) | |
154 | { | |
155 | TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0)); | |
156 | TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1)); | |
157 | TREE_TYPE (expr) = type; | |
158 | return expr; | |
159 | } | |
f5963e61 | 160 | |
c9529354 RK |
161 | else if (ex_form == TRUTH_NOT_EXPR) |
162 | { | |
163 | TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0)); | |
164 | TREE_TYPE (expr) = type; | |
165 | return expr; | |
166 | } | |
f5963e61 JL |
167 | |
168 | /* If we are widening the type, put in an explicit conversion. | |
169 | Similarly if we are not changing the width. After this, we know | |
170 | we are truncating EXPR. */ | |
171 | ||
76e616db BK |
172 | else if (outprec >= inprec) |
173 | return build1 (NOP_EXPR, type, expr); | |
174 | ||
1c013b45 RK |
175 | /* If TYPE is an enumeral type or a type with a precision less |
176 | than the number of bits in its mode, do the conversion to the | |
177 | type corresponding to its mode, then do a nop conversion | |
178 | to TYPE. */ | |
179 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
180 | || outprec != GET_MODE_BITSIZE (TYPE_MODE (type))) | |
181 | return build1 (NOP_EXPR, type, | |
182 | convert (type_for_mode (TYPE_MODE (type), | |
183 | TREE_UNSIGNED (type)), | |
184 | expr)); | |
185 | ||
ab29fdfc RK |
186 | /* Here detect when we can distribute the truncation down past some |
187 | arithmetic. For example, if adding two longs and converting to an | |
188 | int, we can equally well convert both to ints and then add. | |
189 | For the operations handled here, such truncation distribution | |
190 | is always safe. | |
191 | It is desirable in these cases: | |
192 | 1) when truncating down to full-word from a larger size | |
193 | 2) when truncating takes no work. | |
194 | 3) when at least one operand of the arithmetic has been extended | |
195 | (as by C's default conversions). In this case we need two conversions | |
196 | if we do the arithmetic as already requested, so we might as well | |
197 | truncate both and then combine. Perhaps that way we need only one. | |
198 | ||
199 | Note that in general we cannot do the arithmetic in a type | |
200 | shorter than the desired result of conversion, even if the operands | |
201 | are both extended from a shorter type, because they might overflow | |
202 | if combined in that type. The exceptions to this--the times when | |
203 | two narrow values can be combined in their narrow type even to | |
204 | make a wider result--are handled by "shorten" in build_binary_op. */ | |
76e616db BK |
205 | |
206 | switch (ex_form) | |
207 | { | |
208 | case RSHIFT_EXPR: | |
209 | /* We can pass truncation down through right shifting | |
210 | when the shift count is a nonpositive constant. */ | |
211 | if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST | |
ab29fdfc RK |
212 | && tree_int_cst_lt (TREE_OPERAND (expr, 1), |
213 | convert (TREE_TYPE (TREE_OPERAND (expr, 1)), | |
214 | integer_one_node))) | |
76e616db BK |
215 | goto trunc1; |
216 | break; | |
217 | ||
218 | case LSHIFT_EXPR: | |
219 | /* We can pass truncation down through left shifting | |
220 | when the shift count is a nonnegative constant. */ | |
221 | if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST | |
ab29fdfc | 222 | && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0 |
76e616db BK |
223 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
224 | { | |
225 | /* If shift count is less than the width of the truncated type, | |
226 | really shift. */ | |
227 | if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type))) | |
228 | /* In this case, shifting is like multiplication. */ | |
229 | goto trunc1; | |
230 | else | |
d9a9c5a7 RK |
231 | { |
232 | /* If it is >= that width, result is zero. | |
233 | Handling this with trunc1 would give the wrong result: | |
234 | (int) ((long long) a << 32) is well defined (as 0) | |
235 | but (int) a << 32 is undefined and would get a | |
236 | warning. */ | |
237 | ||
238 | tree t = convert_to_integer (type, integer_zero_node); | |
239 | ||
240 | /* If the original expression had side-effects, we must | |
241 | preserve it. */ | |
242 | if (TREE_SIDE_EFFECTS (expr)) | |
243 | return build (COMPOUND_EXPR, type, expr, t); | |
244 | else | |
245 | return t; | |
246 | } | |
76e616db BK |
247 | } |
248 | break; | |
249 | ||
250 | case MAX_EXPR: | |
251 | case MIN_EXPR: | |
252 | case MULT_EXPR: | |
253 | { | |
254 | tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type); | |
255 | tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type); | |
256 | ||
257 | /* Don't distribute unless the output precision is at least as big | |
258 | as the actual inputs. Otherwise, the comparison of the | |
259 | truncated values will be wrong. */ | |
260 | if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0)) | |
261 | && outprec >= TYPE_PRECISION (TREE_TYPE (arg1)) | |
262 | /* If signedness of arg0 and arg1 don't match, | |
263 | we can't necessarily find a type to compare them in. */ | |
264 | && (TREE_UNSIGNED (TREE_TYPE (arg0)) | |
265 | == TREE_UNSIGNED (TREE_TYPE (arg1)))) | |
266 | goto trunc1; | |
267 | break; | |
268 | } | |
269 | ||
270 | case PLUS_EXPR: | |
271 | case MINUS_EXPR: | |
272 | case BIT_AND_EXPR: | |
273 | case BIT_IOR_EXPR: | |
274 | case BIT_XOR_EXPR: | |
275 | case BIT_ANDTC_EXPR: | |
276 | trunc1: | |
277 | { | |
278 | tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type); | |
279 | tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type); | |
280 | ||
281 | if (outprec >= BITS_PER_WORD | |
282 | || TRULY_NOOP_TRUNCATION (outprec, inprec) | |
283 | || inprec > TYPE_PRECISION (TREE_TYPE (arg0)) | |
284 | || inprec > TYPE_PRECISION (TREE_TYPE (arg1))) | |
285 | { | |
286 | /* Do the arithmetic in type TYPEX, | |
287 | then convert result to TYPE. */ | |
288 | register tree typex = type; | |
289 | ||
290 | /* Can't do arithmetic in enumeral types | |
291 | so use an integer type that will hold the values. */ | |
292 | if (TREE_CODE (typex) == ENUMERAL_TYPE) | |
293 | typex = type_for_size (TYPE_PRECISION (typex), | |
294 | TREE_UNSIGNED (typex)); | |
295 | ||
296 | /* But now perhaps TYPEX is as wide as INPREC. | |
297 | In that case, do nothing special here. | |
298 | (Otherwise would recurse infinitely in convert. */ | |
299 | if (TYPE_PRECISION (typex) != inprec) | |
300 | { | |
301 | /* Don't do unsigned arithmetic where signed was wanted, | |
302 | or vice versa. | |
303 | Exception: if either of the original operands were | |
304 | unsigned then can safely do the work as unsigned. | |
305 | And we may need to do it as unsigned | |
306 | if we truncate to the original size. */ | |
307 | typex = ((TREE_UNSIGNED (TREE_TYPE (expr)) | |
308 | || TREE_UNSIGNED (TREE_TYPE (arg0)) | |
309 | || TREE_UNSIGNED (TREE_TYPE (arg1))) | |
310 | ? unsigned_type (typex) : signed_type (typex)); | |
311 | return convert (type, | |
95e78909 RK |
312 | fold (build (ex_form, typex, |
313 | convert (typex, arg0), | |
314 | convert (typex, arg1), | |
315 | 0))); | |
76e616db BK |
316 | } |
317 | } | |
318 | } | |
319 | break; | |
320 | ||
321 | case NEGATE_EXPR: | |
322 | case BIT_NOT_EXPR: | |
d283912a RS |
323 | /* This is not correct for ABS_EXPR, |
324 | since we must test the sign before truncation. */ | |
76e616db BK |
325 | { |
326 | register tree typex = type; | |
327 | ||
328 | /* Can't do arithmetic in enumeral types | |
329 | so use an integer type that will hold the values. */ | |
330 | if (TREE_CODE (typex) == ENUMERAL_TYPE) | |
331 | typex = type_for_size (TYPE_PRECISION (typex), | |
332 | TREE_UNSIGNED (typex)); | |
333 | ||
334 | /* But now perhaps TYPEX is as wide as INPREC. | |
335 | In that case, do nothing special here. | |
336 | (Otherwise would recurse infinitely in convert. */ | |
337 | if (TYPE_PRECISION (typex) != inprec) | |
338 | { | |
339 | /* Don't do unsigned arithmetic where signed was wanted, | |
340 | or vice versa. */ | |
341 | typex = (TREE_UNSIGNED (TREE_TYPE (expr)) | |
342 | ? unsigned_type (typex) : signed_type (typex)); | |
343 | return convert (type, | |
95e78909 RK |
344 | fold (build1 (ex_form, typex, |
345 | convert (typex, | |
346 | TREE_OPERAND (expr, 0))))); | |
76e616db BK |
347 | } |
348 | } | |
349 | ||
350 | case NOP_EXPR: | |
351 | /* If truncating after truncating, might as well do all at once. | |
352 | If truncating after extending, we may get rid of wasted work. */ | |
353 | return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type)); | |
354 | ||
355 | case COND_EXPR: | |
f5963e61 JL |
356 | /* It is sometimes worthwhile to push the narrowing down through |
357 | the conditional and never loses. */ | |
358 | return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0), | |
359 | convert (type, TREE_OPERAND (expr, 1)), | |
360 | convert (type, TREE_OPERAND (expr, 2)))); | |
76e616db | 361 | |
31031edd JL |
362 | default: |
363 | break; | |
76e616db BK |
364 | } |
365 | ||
366 | return build1 (NOP_EXPR, type, expr); | |
76e616db | 367 | |
f5963e61 JL |
368 | case REAL_TYPE: |
369 | return build1 (FIX_TRUNC_EXPR, type, expr); | |
76e616db | 370 | |
f5963e61 JL |
371 | case COMPLEX_TYPE: |
372 | return convert (type, | |
373 | fold (build1 (REALPART_EXPR, | |
374 | TREE_TYPE (TREE_TYPE (expr)), expr))); | |
0b127821 | 375 | |
f5963e61 JL |
376 | default: |
377 | error ("aggregate value used where an integer was expected"); | |
378 | return convert (type, integer_zero_node); | |
379 | } | |
76e616db | 380 | } |
0b127821 RS |
381 | |
382 | /* Convert EXPR to the complex type TYPE in the usual ways. */ | |
383 | ||
384 | tree | |
385 | convert_to_complex (type, expr) | |
386 | tree type, expr; | |
387 | { | |
0b127821 RS |
388 | tree subtype = TREE_TYPE (type); |
389 | ||
f5963e61 | 390 | switch (TREE_CODE (TREE_TYPE (expr))) |
0b127821 | 391 | { |
f5963e61 JL |
392 | case REAL_TYPE: |
393 | case INTEGER_TYPE: | |
394 | case ENUMERAL_TYPE: | |
395 | case BOOLEAN_TYPE: | |
396 | case CHAR_TYPE: | |
397 | return build (COMPLEX_EXPR, type, convert (subtype, expr), | |
0b127821 | 398 | convert (subtype, integer_zero_node)); |
0b127821 | 399 | |
f5963e61 JL |
400 | case COMPLEX_TYPE: |
401 | { | |
402 | tree elt_type = TREE_TYPE (TREE_TYPE (expr)); | |
403 | ||
404 | if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype)) | |
405 | return expr; | |
406 | else if (TREE_CODE (expr) == COMPLEX_EXPR) | |
0b127821 RS |
407 | return fold (build (COMPLEX_EXPR, |
408 | type, | |
f5963e61 JL |
409 | convert (subtype, TREE_OPERAND (expr, 0)), |
410 | convert (subtype, TREE_OPERAND (expr, 1)))); | |
411 | else | |
412 | { | |
413 | expr = save_expr (expr); | |
414 | return | |
415 | fold (build (COMPLEX_EXPR, | |
416 | type, convert (subtype, | |
417 | fold (build1 (REALPART_EXPR, | |
418 | TREE_TYPE (TREE_TYPE (expr)), | |
419 | expr))), | |
420 | convert (subtype, | |
421 | fold (build1 (IMAGPART_EXPR, | |
422 | TREE_TYPE (TREE_TYPE (expr)), | |
423 | expr))))); | |
424 | } | |
425 | } | |
0b127821 | 426 | |
f5963e61 JL |
427 | case POINTER_TYPE: |
428 | case REFERENCE_TYPE: | |
429 | error ("pointer value used where a complex was expected"); | |
430 | return convert_to_complex (type, integer_zero_node); | |
431 | ||
432 | default: | |
433 | error ("aggregate value used where a complex was expected"); | |
434 | return convert_to_complex (type, integer_zero_node); | |
435 | } | |
0b127821 | 436 | } |