1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997,
3 1998 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "toplev.h"
32 #include "langhooks.h"
33
34 /* Convert EXPR to some pointer or reference type TYPE.
35
36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
37 in other cases error is called. */
38
39 tree
convert_to_pointer(type,expr)40 convert_to_pointer (type, expr)
41 tree type, expr;
42 {
43 if (integer_zerop (expr))
44 {
45 expr = build_int_2 (0, 0);
46 TREE_TYPE (expr) = type;
47 return expr;
48 }
49
50 switch (TREE_CODE (TREE_TYPE (expr)))
51 {
52 case POINTER_TYPE:
53 case REFERENCE_TYPE:
54 return build1 (NOP_EXPR, type, expr);
55
56 case INTEGER_TYPE:
57 case ENUMERAL_TYPE:
58 case BOOLEAN_TYPE:
59 case CHAR_TYPE:
60 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
61 return build1 (CONVERT_EXPR, type, expr);
62
63 return
64 convert_to_pointer (type,
65 convert ((*lang_hooks.types.type_for_size)
66 (POINTER_SIZE, 0), expr));
67
68 default:
69 error ("cannot convert to a pointer type");
70 return convert_to_pointer (type, integer_zero_node);
71 }
72 }
73
74 /* Convert EXPR to some floating-point type TYPE.
75
76 EXPR must be float, integer, or enumeral;
77 in other cases error is called. */
78
79 tree
convert_to_real(type,expr)80 convert_to_real (type, expr)
81 tree type, expr;
82 {
83 switch (TREE_CODE (TREE_TYPE (expr)))
84 {
85 case REAL_TYPE:
86 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
87 type, expr);
88
89 case INTEGER_TYPE:
90 case ENUMERAL_TYPE:
91 case BOOLEAN_TYPE:
92 case CHAR_TYPE:
93 return build1 (FLOAT_EXPR, type, expr);
94
95 case COMPLEX_TYPE:
96 return convert (type,
97 fold (build1 (REALPART_EXPR,
98 TREE_TYPE (TREE_TYPE (expr)), expr)));
99
100 case POINTER_TYPE:
101 case REFERENCE_TYPE:
102 error ("pointer value used where a floating point value was expected");
103 return convert_to_real (type, integer_zero_node);
104
105 default:
106 error ("aggregate value used where a float was expected");
107 return convert_to_real (type, integer_zero_node);
108 }
109 }
110
111 /* Convert EXPR to some integer (or enum) type TYPE.
112
113 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
114 vector; in other cases error is called.
115
116 The result of this is always supposed to be a newly created tree node
117 not in use in any existing structure. */
118
119 tree
convert_to_integer(type,expr)120 convert_to_integer (type, expr)
121 tree type, expr;
122 {
123 enum tree_code ex_form = TREE_CODE (expr);
124 tree intype = TREE_TYPE (expr);
125 unsigned int inprec = TYPE_PRECISION (intype);
126 unsigned int outprec = TYPE_PRECISION (type);
127
128 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
129 be. Consider `enum E = { a, b = (enum E) 3 };'. */
130 if (!COMPLETE_TYPE_P (type))
131 {
132 error ("conversion to incomplete type");
133 return error_mark_node;
134 }
135
136 switch (TREE_CODE (intype))
137 {
138 case POINTER_TYPE:
139 case REFERENCE_TYPE:
140 if (integer_zerop (expr))
141 expr = integer_zero_node;
142 else
143 expr = fold (build1 (CONVERT_EXPR, (*lang_hooks.types.type_for_size)
144 (POINTER_SIZE, 0), expr));
145
146 return convert_to_integer (type, expr);
147
148 case INTEGER_TYPE:
149 case ENUMERAL_TYPE:
150 case BOOLEAN_TYPE:
151 case CHAR_TYPE:
152 /* If this is a logical operation, which just returns 0 or 1, we can
153 change the type of the expression. For some logical operations,
154 we must also change the types of the operands to maintain type
155 correctness. */
156
157 if (TREE_CODE_CLASS (ex_form) == '<')
158 {
159 TREE_TYPE (expr) = type;
160 return expr;
161 }
162
163 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
164 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
165 || ex_form == TRUTH_XOR_EXPR)
166 {
167 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
168 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
169 TREE_TYPE (expr) = type;
170 return expr;
171 }
172
173 else if (ex_form == TRUTH_NOT_EXPR)
174 {
175 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
176 TREE_TYPE (expr) = type;
177 return expr;
178 }
179
180 /* If we are widening the type, put in an explicit conversion.
181 Similarly if we are not changing the width. After this, we know
182 we are truncating EXPR. */
183
184 else if (outprec >= inprec)
185 {
186 enum tree_code code;
187
188 /* If the precision of the EXPR's type is K bits and the
189 destination mode has more bits, and the sign is changing,
190 it is not safe to use a NOP_EXPR. For example, suppose
191 that EXPR's type is a 3-bit unsigned integer type, the
192 TYPE is a 3-bit signed integer type, and the machine mode
193 for the types is 8-bit QImode. In that case, the
194 conversion necessitates an explicit sign-extension. In
195 the signed-to-unsigned case the high-order bits have to
196 be cleared. */
197 if (TREE_UNSIGNED (type) != TREE_UNSIGNED (TREE_TYPE (expr))
198 && (TYPE_PRECISION (TREE_TYPE (expr))
199 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
200 code = CONVERT_EXPR;
201 else
202 code = NOP_EXPR;
203
204 return build1 (code, type, expr);
205 }
206
207 /* If TYPE is an enumeral type or a type with a precision less
208 than the number of bits in its mode, do the conversion to the
209 type corresponding to its mode, then do a nop conversion
210 to TYPE. */
211 else if (TREE_CODE (type) == ENUMERAL_TYPE
212 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
213 return build1 (NOP_EXPR, type,
214 convert ((*lang_hooks.types.type_for_mode)
215 (TYPE_MODE (type), TREE_UNSIGNED (type)),
216 expr));
217
218 /* Here detect when we can distribute the truncation down past some
219 arithmetic. For example, if adding two longs and converting to an
220 int, we can equally well convert both to ints and then add.
221 For the operations handled here, such truncation distribution
222 is always safe.
223 It is desirable in these cases:
224 1) when truncating down to full-word from a larger size
225 2) when truncating takes no work.
226 3) when at least one operand of the arithmetic has been extended
227 (as by C's default conversions). In this case we need two conversions
228 if we do the arithmetic as already requested, so we might as well
229 truncate both and then combine. Perhaps that way we need only one.
230
231 Note that in general we cannot do the arithmetic in a type
232 shorter than the desired result of conversion, even if the operands
233 are both extended from a shorter type, because they might overflow
234 if combined in that type. The exceptions to this--the times when
235 two narrow values can be combined in their narrow type even to
236 make a wider result--are handled by "shorten" in build_binary_op. */
237
238 switch (ex_form)
239 {
240 case RSHIFT_EXPR:
241 /* We can pass truncation down through right shifting
242 when the shift count is a nonpositive constant. */
243 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
244 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
245 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
246 integer_one_node)))
247 goto trunc1;
248 break;
249
250 case LSHIFT_EXPR:
251 /* We can pass truncation down through left shifting
252 when the shift count is a nonnegative constant and
253 the target type is unsigned. */
254 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
255 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
256 && TREE_UNSIGNED (type)
257 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
258 {
259 /* If shift count is less than the width of the truncated type,
260 really shift. */
261 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
262 /* In this case, shifting is like multiplication. */
263 goto trunc1;
264 else
265 {
266 /* If it is >= that width, result is zero.
267 Handling this with trunc1 would give the wrong result:
268 (int) ((long long) a << 32) is well defined (as 0)
269 but (int) a << 32 is undefined and would get a
270 warning. */
271
272 tree t = convert_to_integer (type, integer_zero_node);
273
274 /* If the original expression had side-effects, we must
275 preserve it. */
276 if (TREE_SIDE_EFFECTS (expr))
277 return build (COMPOUND_EXPR, type, expr, t);
278 else
279 return t;
280 }
281 }
282 break;
283
284 case MAX_EXPR:
285 case MIN_EXPR:
286 case MULT_EXPR:
287 {
288 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
289 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
290
291 /* Don't distribute unless the output precision is at least as big
292 as the actual inputs. Otherwise, the comparison of the
293 truncated values will be wrong. */
294 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
295 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
296 /* If signedness of arg0 and arg1 don't match,
297 we can't necessarily find a type to compare them in. */
298 && (TREE_UNSIGNED (TREE_TYPE (arg0))
299 == TREE_UNSIGNED (TREE_TYPE (arg1))))
300 goto trunc1;
301 break;
302 }
303
304 case PLUS_EXPR:
305 case MINUS_EXPR:
306 case BIT_AND_EXPR:
307 case BIT_IOR_EXPR:
308 case BIT_XOR_EXPR:
309 case BIT_ANDTC_EXPR:
310 trunc1:
311 {
312 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
313 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
314
315 if (outprec >= BITS_PER_WORD
316 || TRULY_NOOP_TRUNCATION (outprec, inprec)
317 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
318 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
319 {
320 /* Do the arithmetic in type TYPEX,
321 then convert result to TYPE. */
322 tree typex = type;
323
324 /* Can't do arithmetic in enumeral types
325 so use an integer type that will hold the values. */
326 if (TREE_CODE (typex) == ENUMERAL_TYPE)
327 typex = (*lang_hooks.types.type_for_size)
328 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
329
330 /* But now perhaps TYPEX is as wide as INPREC.
331 In that case, do nothing special here.
332 (Otherwise would recurse infinitely in convert. */
333 if (TYPE_PRECISION (typex) != inprec)
334 {
335 /* Don't do unsigned arithmetic where signed was wanted,
336 or vice versa.
337 Exception: if both of the original operands were
338 unsigned then we can safely do the work as unsigned.
339 Exception: shift operations take their type solely
340 from the first argument.
341 Exception: the LSHIFT_EXPR case above requires that
342 we perform this operation unsigned lest we produce
343 signed-overflow undefinedness.
344 And we may need to do it as unsigned
345 if we truncate to the original size. */
346 if (TREE_UNSIGNED (TREE_TYPE (expr))
347 || (TREE_UNSIGNED (TREE_TYPE (arg0))
348 && (TREE_UNSIGNED (TREE_TYPE (arg1))
349 || ex_form == LSHIFT_EXPR
350 || ex_form == RSHIFT_EXPR
351 || ex_form == LROTATE_EXPR
352 || ex_form == RROTATE_EXPR))
353 || ex_form == LSHIFT_EXPR)
354 typex = (*lang_hooks.types.unsigned_type) (typex);
355 else
356 typex = (*lang_hooks.types.signed_type) (typex);
357 return convert (type,
358 fold (build (ex_form, typex,
359 convert (typex, arg0),
360 convert (typex, arg1),
361 0)));
362 }
363 }
364 }
365 break;
366
367 case NEGATE_EXPR:
368 case BIT_NOT_EXPR:
369 /* This is not correct for ABS_EXPR,
370 since we must test the sign before truncation. */
371 {
372 tree typex = type;
373
374 /* Can't do arithmetic in enumeral types
375 so use an integer type that will hold the values. */
376 if (TREE_CODE (typex) == ENUMERAL_TYPE)
377 typex = (*lang_hooks.types.type_for_size)
378 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
379
380 /* But now perhaps TYPEX is as wide as INPREC.
381 In that case, do nothing special here.
382 (Otherwise would recurse infinitely in convert. */
383 if (TYPE_PRECISION (typex) != inprec)
384 {
385 /* Don't do unsigned arithmetic where signed was wanted,
386 or vice versa. */
387 if (TREE_UNSIGNED (TREE_TYPE (expr)))
388 typex = (*lang_hooks.types.unsigned_type) (typex);
389 else
390 typex = (*lang_hooks.types.signed_type) (typex);
391 return convert (type,
392 fold (build1 (ex_form, typex,
393 convert (typex,
394 TREE_OPERAND (expr, 0)))));
395 }
396 }
397
398 case NOP_EXPR:
399 /* Don't introduce a
400 "can't convert between vector values of different size" error. */
401 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
402 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
403 != GET_MODE_SIZE (TYPE_MODE (type))))
404 break;
405 /* If truncating after truncating, might as well do all at once.
406 If truncating after extending, we may get rid of wasted work. */
407 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
408
409 case COND_EXPR:
410 /* It is sometimes worthwhile to push the narrowing down through
411 the conditional and never loses. */
412 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
413 convert (type, TREE_OPERAND (expr, 1)),
414 convert (type, TREE_OPERAND (expr, 2))));
415
416 default:
417 break;
418 }
419
420 return build1 (NOP_EXPR, type, expr);
421
422 case REAL_TYPE:
423 return build1 (FIX_TRUNC_EXPR, type, expr);
424
425 case COMPLEX_TYPE:
426 return convert (type,
427 fold (build1 (REALPART_EXPR,
428 TREE_TYPE (TREE_TYPE (expr)), expr)));
429
430 case VECTOR_TYPE:
431 if (GET_MODE_SIZE (TYPE_MODE (type))
432 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
433 {
434 error ("can't convert between vector values of different size");
435 return error_mark_node;
436 }
437 return build1 (NOP_EXPR, type, expr);
438
439 default:
440 error ("aggregate value used where an integer was expected");
441 return convert (type, integer_zero_node);
442 }
443 }
444
445 /* Convert EXPR to the complex type TYPE in the usual ways. */
446
447 tree
convert_to_complex(type,expr)448 convert_to_complex (type, expr)
449 tree type, expr;
450 {
451 tree subtype = TREE_TYPE (type);
452
453 switch (TREE_CODE (TREE_TYPE (expr)))
454 {
455 case REAL_TYPE:
456 case INTEGER_TYPE:
457 case ENUMERAL_TYPE:
458 case BOOLEAN_TYPE:
459 case CHAR_TYPE:
460 return build (COMPLEX_EXPR, type, convert (subtype, expr),
461 convert (subtype, integer_zero_node));
462
463 case COMPLEX_TYPE:
464 {
465 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
466
467 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
468 return expr;
469 else if (TREE_CODE (expr) == COMPLEX_EXPR)
470 return fold (build (COMPLEX_EXPR,
471 type,
472 convert (subtype, TREE_OPERAND (expr, 0)),
473 convert (subtype, TREE_OPERAND (expr, 1))));
474 else
475 {
476 expr = save_expr (expr);
477 return
478 fold (build (COMPLEX_EXPR,
479 type, convert (subtype,
480 fold (build1 (REALPART_EXPR,
481 TREE_TYPE (TREE_TYPE (expr)),
482 expr))),
483 convert (subtype,
484 fold (build1 (IMAGPART_EXPR,
485 TREE_TYPE (TREE_TYPE (expr)),
486 expr)))));
487 }
488 }
489
490 case POINTER_TYPE:
491 case REFERENCE_TYPE:
492 error ("pointer value used where a complex was expected");
493 return convert_to_complex (type, integer_zero_node);
494
495 default:
496 error ("aggregate value used where a complex was expected");
497 return convert_to_complex (type, integer_zero_node);
498 }
499 }
500
501 /* Convert EXPR to the vector type TYPE in the usual ways. */
502
503 tree
convert_to_vector(type,expr)504 convert_to_vector (type, expr)
505 tree type, expr;
506 {
507 switch (TREE_CODE (TREE_TYPE (expr)))
508 {
509 case INTEGER_TYPE:
510 case VECTOR_TYPE:
511 if (GET_MODE_SIZE (TYPE_MODE (type))
512 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
513 {
514 error ("can't convert between vector values of different size");
515 return error_mark_node;
516 }
517 return build1 (NOP_EXPR, type, expr);
518
519 default:
520 error ("can't convert value to a vector");
521 return convert_to_vector (type, integer_zero_node);
522 }
523 }
524