2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / convert.c
blob0fef3a2e7465a42fe0b327ca20af351984c0230a
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "convert.h"
33 #include "toplev.h"
34 #include "langhooks.h"
35 #include "real.h"
36 #include "fixed-value.h"
38 /* Convert EXPR to some pointer or reference type TYPE.
39 EXPR must be pointer, reference, integer, enumeral, or literal zero;
40 in other cases error is called. */
42 tree
43 convert_to_pointer (tree type, tree expr)
45 if (TREE_TYPE (expr) == type)
46 return expr;
48 /* Propagate overflow to the NULL pointer. */
49 if (integer_zerop (expr))
50 return force_fit_type_double (type, 0, 0, 0, TREE_OVERFLOW (expr));
52 switch (TREE_CODE (TREE_TYPE (expr)))
54 case POINTER_TYPE:
55 case REFERENCE_TYPE:
56 return fold_build1 (NOP_EXPR, type, expr);
58 case INTEGER_TYPE:
59 case ENUMERAL_TYPE:
60 case BOOLEAN_TYPE:
61 if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
62 expr = fold_build1 (NOP_EXPR,
63 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
64 expr);
65 return fold_build1 (CONVERT_EXPR, type, expr);
68 default:
69 error ("cannot convert to a pointer type");
70 return convert_to_pointer (type, integer_zero_node);
74 /* Avoid any floating point extensions from EXP. */
75 tree
76 strip_float_extensions (tree exp)
78 tree sub, expt, subt;
80 /* For floating point constant look up the narrowest type that can hold
81 it properly and handle it like (type)(narrowest_type)constant.
82 This way we can optimize for instance a=a*2.0 where "a" is float
83 but 2.0 is double constant. */
84 if (TREE_CODE (exp) == REAL_CST)
86 REAL_VALUE_TYPE orig;
87 tree type = NULL;
89 orig = TREE_REAL_CST (exp);
90 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
91 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
92 type = float_type_node;
93 else if (TYPE_PRECISION (TREE_TYPE (exp))
94 > TYPE_PRECISION (double_type_node)
95 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
96 type = double_type_node;
97 if (type)
98 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
101 if (!CONVERT_EXPR_P (exp))
102 return exp;
104 sub = TREE_OPERAND (exp, 0);
105 subt = TREE_TYPE (sub);
106 expt = TREE_TYPE (exp);
108 if (!FLOAT_TYPE_P (subt))
109 return exp;
111 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
112 return exp;
114 return strip_float_extensions (sub);
118 /* Convert EXPR to some floating-point type TYPE.
120 EXPR must be float, fixed-point, integer, or enumeral;
121 in other cases error is called. */
123 tree
124 convert_to_real (tree type, tree expr)
126 enum built_in_function fcode = builtin_mathfn_code (expr);
127 tree itype = TREE_TYPE (expr);
129 /* Disable until we figure out how to decide whether the functions are
130 present in runtime. */
131 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
132 if (optimize
133 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
134 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
136 switch (fcode)
138 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
139 CASE_MATHFN (ACOS)
140 CASE_MATHFN (ACOSH)
141 CASE_MATHFN (ASIN)
142 CASE_MATHFN (ASINH)
143 CASE_MATHFN (ATAN)
144 CASE_MATHFN (ATANH)
145 CASE_MATHFN (CBRT)
146 CASE_MATHFN (COS)
147 CASE_MATHFN (COSH)
148 CASE_MATHFN (ERF)
149 CASE_MATHFN (ERFC)
150 CASE_MATHFN (EXP)
151 CASE_MATHFN (EXP10)
152 CASE_MATHFN (EXP2)
153 CASE_MATHFN (EXPM1)
154 CASE_MATHFN (FABS)
155 CASE_MATHFN (GAMMA)
156 CASE_MATHFN (J0)
157 CASE_MATHFN (J1)
158 CASE_MATHFN (LGAMMA)
159 CASE_MATHFN (LOG)
160 CASE_MATHFN (LOG10)
161 CASE_MATHFN (LOG1P)
162 CASE_MATHFN (LOG2)
163 CASE_MATHFN (LOGB)
164 CASE_MATHFN (POW10)
165 CASE_MATHFN (SIN)
166 CASE_MATHFN (SINH)
167 CASE_MATHFN (SQRT)
168 CASE_MATHFN (TAN)
169 CASE_MATHFN (TANH)
170 CASE_MATHFN (TGAMMA)
171 CASE_MATHFN (Y0)
172 CASE_MATHFN (Y1)
173 #undef CASE_MATHFN
175 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
176 tree newtype = type;
178 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
179 the both as the safe type for operation. */
180 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
181 newtype = TREE_TYPE (arg0);
183 /* Be careful about integer to fp conversions.
184 These may overflow still. */
185 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
186 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
187 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
188 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
190 tree fn = mathfn_built_in (newtype, fcode);
192 if (fn)
194 tree arg = fold (convert_to_real (newtype, arg0));
195 expr = build_call_expr (fn, 1, arg);
196 if (newtype == type)
197 return expr;
201 default:
202 break;
205 if (optimize
206 && (((fcode == BUILT_IN_FLOORL
207 || fcode == BUILT_IN_CEILL
208 || fcode == BUILT_IN_ROUNDL
209 || fcode == BUILT_IN_RINTL
210 || fcode == BUILT_IN_TRUNCL
211 || fcode == BUILT_IN_NEARBYINTL)
212 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
213 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
214 || ((fcode == BUILT_IN_FLOOR
215 || fcode == BUILT_IN_CEIL
216 || fcode == BUILT_IN_ROUND
217 || fcode == BUILT_IN_RINT
218 || fcode == BUILT_IN_TRUNC
219 || fcode == BUILT_IN_NEARBYINT)
220 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
222 tree fn = mathfn_built_in (type, fcode);
224 if (fn)
226 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
228 /* Make sure (type)arg0 is an extension, otherwise we could end up
229 changing (float)floor(double d) into floorf((float)d), which is
230 incorrect because (float)d uses round-to-nearest and can round
231 up to the next integer. */
232 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
233 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
237 /* Propagate the cast into the operation. */
238 if (itype != type && FLOAT_TYPE_P (type))
239 switch (TREE_CODE (expr))
241 /* Convert (float)-x into -(float)x. This is safe for
242 round-to-nearest rounding mode. */
243 case ABS_EXPR:
244 case NEGATE_EXPR:
245 if (!flag_rounding_math
246 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
247 return build1 (TREE_CODE (expr), type,
248 fold (convert_to_real (type,
249 TREE_OPERAND (expr, 0))));
250 break;
251 /* Convert (outertype)((innertype0)a+(innertype1)b)
252 into ((newtype)a+(newtype)b) where newtype
253 is the widest mode from all of these. */
254 case PLUS_EXPR:
255 case MINUS_EXPR:
256 case MULT_EXPR:
257 case RDIV_EXPR:
259 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
260 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
262 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
263 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
265 tree newtype = type;
267 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
268 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode)
269 newtype = dfloat32_type_node;
270 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
271 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode)
272 newtype = dfloat64_type_node;
273 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
274 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode)
275 newtype = dfloat128_type_node;
276 if (newtype == dfloat32_type_node
277 || newtype == dfloat64_type_node
278 || newtype == dfloat128_type_node)
280 expr = build2 (TREE_CODE (expr), newtype,
281 fold (convert_to_real (newtype, arg0)),
282 fold (convert_to_real (newtype, arg1)));
283 if (newtype == type)
284 return expr;
285 break;
288 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
289 newtype = TREE_TYPE (arg0);
290 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
291 newtype = TREE_TYPE (arg1);
292 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
294 expr = build2 (TREE_CODE (expr), newtype,
295 fold (convert_to_real (newtype, arg0)),
296 fold (convert_to_real (newtype, arg1)));
297 if (newtype == type)
298 return expr;
302 break;
303 default:
304 break;
307 switch (TREE_CODE (TREE_TYPE (expr)))
309 case REAL_TYPE:
310 /* Ignore the conversion if we don't need to store intermediate
311 results and neither type is a decimal float. */
312 return build1 ((flag_float_store
313 || DECIMAL_FLOAT_TYPE_P (type)
314 || DECIMAL_FLOAT_TYPE_P (itype))
315 ? CONVERT_EXPR : NOP_EXPR, type, expr);
317 case INTEGER_TYPE:
318 case ENUMERAL_TYPE:
319 case BOOLEAN_TYPE:
320 return build1 (FLOAT_EXPR, type, expr);
322 case FIXED_POINT_TYPE:
323 return build1 (FIXED_CONVERT_EXPR, type, expr);
325 case COMPLEX_TYPE:
326 return convert (type,
327 fold_build1 (REALPART_EXPR,
328 TREE_TYPE (TREE_TYPE (expr)), expr));
330 case POINTER_TYPE:
331 case REFERENCE_TYPE:
332 error ("pointer value used where a floating point value was expected");
333 return convert_to_real (type, integer_zero_node);
335 default:
336 error ("aggregate value used where a float was expected");
337 return convert_to_real (type, integer_zero_node);
341 /* Convert EXPR to some integer (or enum) type TYPE.
343 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
344 fixed-point or vector; in other cases error is called.
346 The result of this is always supposed to be a newly created tree node
347 not in use in any existing structure. */
349 tree
350 convert_to_integer (tree type, tree expr)
352 enum tree_code ex_form = TREE_CODE (expr);
353 tree intype = TREE_TYPE (expr);
354 unsigned int inprec = TYPE_PRECISION (intype);
355 unsigned int outprec = TYPE_PRECISION (type);
357 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
358 be. Consider `enum E = { a, b = (enum E) 3 };'. */
359 if (!COMPLETE_TYPE_P (type))
361 error ("conversion to incomplete type");
362 return error_mark_node;
365 /* Convert e.g. (long)round(d) -> lround(d). */
366 /* If we're converting to char, we may encounter differing behavior
367 between converting from double->char vs double->long->char.
368 We're in "undefined" territory but we prefer to be conservative,
369 so only proceed in "unsafe" math mode. */
370 if (optimize
371 && (flag_unsafe_math_optimizations
372 || (long_integer_type_node
373 && outprec >= TYPE_PRECISION (long_integer_type_node))))
375 tree s_expr = strip_float_extensions (expr);
376 tree s_intype = TREE_TYPE (s_expr);
377 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
378 tree fn = 0;
380 switch (fcode)
382 CASE_FLT_FN (BUILT_IN_CEIL):
383 /* Only convert in ISO C99 mode. */
384 if (!TARGET_C99_FUNCTIONS)
385 break;
386 if (outprec < TYPE_PRECISION (long_integer_type_node)
387 || (outprec == TYPE_PRECISION (long_integer_type_node)
388 && !TYPE_UNSIGNED (type)))
389 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
390 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
391 && !TYPE_UNSIGNED (type))
392 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
393 break;
395 CASE_FLT_FN (BUILT_IN_FLOOR):
396 /* Only convert in ISO C99 mode. */
397 if (!TARGET_C99_FUNCTIONS)
398 break;
399 if (outprec < TYPE_PRECISION (long_integer_type_node)
400 || (outprec == TYPE_PRECISION (long_integer_type_node)
401 && !TYPE_UNSIGNED (type)))
402 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
403 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
404 && !TYPE_UNSIGNED (type))
405 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
406 break;
408 CASE_FLT_FN (BUILT_IN_ROUND):
409 if (outprec < TYPE_PRECISION (long_integer_type_node)
410 || (outprec == TYPE_PRECISION (long_integer_type_node)
411 && !TYPE_UNSIGNED (type)))
412 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
413 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
414 && !TYPE_UNSIGNED (type))
415 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
416 break;
418 CASE_FLT_FN (BUILT_IN_NEARBYINT):
419 /* Only convert nearbyint* if we can ignore math exceptions. */
420 if (flag_trapping_math)
421 break;
422 /* ... Fall through ... */
423 CASE_FLT_FN (BUILT_IN_RINT):
424 if (outprec < TYPE_PRECISION (long_integer_type_node)
425 || (outprec == TYPE_PRECISION (long_integer_type_node)
426 && !TYPE_UNSIGNED (type)))
427 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
428 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
429 && !TYPE_UNSIGNED (type))
430 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
431 break;
433 CASE_FLT_FN (BUILT_IN_TRUNC):
434 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
436 default:
437 break;
440 if (fn)
442 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
443 return convert_to_integer (type, newexpr);
447 switch (TREE_CODE (intype))
449 case POINTER_TYPE:
450 case REFERENCE_TYPE:
451 if (integer_zerop (expr))
452 return build_int_cst (type, 0);
454 /* Convert to an unsigned integer of the correct width first,
455 and from there widen/truncate to the required type. */
456 expr = fold_build1 (CONVERT_EXPR,
457 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
458 expr);
459 return fold_convert (type, expr);
461 case INTEGER_TYPE:
462 case ENUMERAL_TYPE:
463 case BOOLEAN_TYPE:
464 /* If this is a logical operation, which just returns 0 or 1, we can
465 change the type of the expression. */
467 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
469 expr = copy_node (expr);
470 TREE_TYPE (expr) = type;
471 return expr;
474 /* If we are widening the type, put in an explicit conversion.
475 Similarly if we are not changing the width. After this, we know
476 we are truncating EXPR. */
478 else if (outprec >= inprec)
480 enum tree_code code;
481 tree tem;
483 /* If the precision of the EXPR's type is K bits and the
484 destination mode has more bits, and the sign is changing,
485 it is not safe to use a NOP_EXPR. For example, suppose
486 that EXPR's type is a 3-bit unsigned integer type, the
487 TYPE is a 3-bit signed integer type, and the machine mode
488 for the types is 8-bit QImode. In that case, the
489 conversion necessitates an explicit sign-extension. In
490 the signed-to-unsigned case the high-order bits have to
491 be cleared. */
492 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
493 && (TYPE_PRECISION (TREE_TYPE (expr))
494 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
495 code = CONVERT_EXPR;
496 else
497 code = NOP_EXPR;
499 tem = fold_unary (code, type, expr);
500 if (tem)
501 return tem;
503 tem = build1 (code, type, expr);
504 TREE_NO_WARNING (tem) = 1;
505 return tem;
508 /* If TYPE is an enumeral type or a type with a precision less
509 than the number of bits in its mode, do the conversion to the
510 type corresponding to its mode, then do a nop conversion
511 to TYPE. */
512 else if (TREE_CODE (type) == ENUMERAL_TYPE
513 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
514 return build1 (NOP_EXPR, type,
515 convert (lang_hooks.types.type_for_mode
516 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
517 expr));
519 /* Here detect when we can distribute the truncation down past some
520 arithmetic. For example, if adding two longs and converting to an
521 int, we can equally well convert both to ints and then add.
522 For the operations handled here, such truncation distribution
523 is always safe.
524 It is desirable in these cases:
525 1) when truncating down to full-word from a larger size
526 2) when truncating takes no work.
527 3) when at least one operand of the arithmetic has been extended
528 (as by C's default conversions). In this case we need two conversions
529 if we do the arithmetic as already requested, so we might as well
530 truncate both and then combine. Perhaps that way we need only one.
532 Note that in general we cannot do the arithmetic in a type
533 shorter than the desired result of conversion, even if the operands
534 are both extended from a shorter type, because they might overflow
535 if combined in that type. The exceptions to this--the times when
536 two narrow values can be combined in their narrow type even to
537 make a wider result--are handled by "shorten" in build_binary_op. */
539 switch (ex_form)
541 case RSHIFT_EXPR:
542 /* We can pass truncation down through right shifting
543 when the shift count is a nonpositive constant. */
544 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
545 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
546 goto trunc1;
547 break;
549 case LSHIFT_EXPR:
550 /* We can pass truncation down through left shifting
551 when the shift count is a nonnegative constant and
552 the target type is unsigned. */
553 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
554 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
555 && TYPE_UNSIGNED (type)
556 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
558 /* If shift count is less than the width of the truncated type,
559 really shift. */
560 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
561 /* In this case, shifting is like multiplication. */
562 goto trunc1;
563 else
565 /* If it is >= that width, result is zero.
566 Handling this with trunc1 would give the wrong result:
567 (int) ((long long) a << 32) is well defined (as 0)
568 but (int) a << 32 is undefined and would get a
569 warning. */
571 tree t = build_int_cst (type, 0);
573 /* If the original expression had side-effects, we must
574 preserve it. */
575 if (TREE_SIDE_EFFECTS (expr))
576 return build2 (COMPOUND_EXPR, type, expr, t);
577 else
578 return t;
581 break;
583 case MAX_EXPR:
584 case MIN_EXPR:
585 case MULT_EXPR:
587 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
588 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
590 /* Don't distribute unless the output precision is at least as big
591 as the actual inputs. Otherwise, the comparison of the
592 truncated values will be wrong. */
593 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
594 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
595 /* If signedness of arg0 and arg1 don't match,
596 we can't necessarily find a type to compare them in. */
597 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
598 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
599 goto trunc1;
600 break;
603 case PLUS_EXPR:
604 case MINUS_EXPR:
605 case BIT_AND_EXPR:
606 case BIT_IOR_EXPR:
607 case BIT_XOR_EXPR:
608 trunc1:
610 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
611 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
613 if (outprec >= BITS_PER_WORD
614 || TRULY_NOOP_TRUNCATION (outprec, inprec)
615 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
616 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
618 /* Do the arithmetic in type TYPEX,
619 then convert result to TYPE. */
620 tree typex = type;
622 /* Can't do arithmetic in enumeral types
623 so use an integer type that will hold the values. */
624 if (TREE_CODE (typex) == ENUMERAL_TYPE)
625 typex = lang_hooks.types.type_for_size
626 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
628 /* But now perhaps TYPEX is as wide as INPREC.
629 In that case, do nothing special here.
630 (Otherwise would recurse infinitely in convert. */
631 if (TYPE_PRECISION (typex) != inprec)
633 /* Don't do unsigned arithmetic where signed was wanted,
634 or vice versa.
635 Exception: if both of the original operands were
636 unsigned then we can safely do the work as unsigned.
637 Exception: shift operations take their type solely
638 from the first argument.
639 Exception: the LSHIFT_EXPR case above requires that
640 we perform this operation unsigned lest we produce
641 signed-overflow undefinedness.
642 And we may need to do it as unsigned
643 if we truncate to the original size. */
644 if (TYPE_UNSIGNED (TREE_TYPE (expr))
645 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
646 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
647 || ex_form == LSHIFT_EXPR
648 || ex_form == RSHIFT_EXPR
649 || ex_form == LROTATE_EXPR
650 || ex_form == RROTATE_EXPR))
651 || ex_form == LSHIFT_EXPR
652 /* If we have !flag_wrapv, and either ARG0 or
653 ARG1 is of a signed type, we have to do
654 PLUS_EXPR or MINUS_EXPR in an unsigned
655 type. Otherwise, we would introduce
656 signed-overflow undefinedness. */
657 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
658 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
659 && (ex_form == PLUS_EXPR
660 || ex_form == MINUS_EXPR)))
661 typex = unsigned_type_for (typex);
662 else
663 typex = signed_type_for (typex);
664 return convert (type,
665 fold_build2 (ex_form, typex,
666 convert (typex, arg0),
667 convert (typex, arg1)));
671 break;
673 case NEGATE_EXPR:
674 case BIT_NOT_EXPR:
675 /* This is not correct for ABS_EXPR,
676 since we must test the sign before truncation. */
678 tree typex;
680 /* Don't do unsigned arithmetic where signed was wanted,
681 or vice versa. */
682 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
683 typex = unsigned_type_for (type);
684 else
685 typex = signed_type_for (type);
686 return convert (type,
687 fold_build1 (ex_form, typex,
688 convert (typex,
689 TREE_OPERAND (expr, 0))));
692 case NOP_EXPR:
693 /* Don't introduce a
694 "can't convert between vector values of different size" error. */
695 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
696 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
697 != GET_MODE_SIZE (TYPE_MODE (type))))
698 break;
699 /* If truncating after truncating, might as well do all at once.
700 If truncating after extending, we may get rid of wasted work. */
701 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
703 case COND_EXPR:
704 /* It is sometimes worthwhile to push the narrowing down through
705 the conditional and never loses. */
706 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
707 convert (type, TREE_OPERAND (expr, 1)),
708 convert (type, TREE_OPERAND (expr, 2)));
710 default:
711 break;
714 return build1 (CONVERT_EXPR, type, expr);
716 case REAL_TYPE:
717 return build1 (FIX_TRUNC_EXPR, type, expr);
719 case FIXED_POINT_TYPE:
720 return build1 (FIXED_CONVERT_EXPR, type, expr);
722 case COMPLEX_TYPE:
723 return convert (type,
724 fold_build1 (REALPART_EXPR,
725 TREE_TYPE (TREE_TYPE (expr)), expr));
727 case VECTOR_TYPE:
728 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
730 error ("can't convert between vector values of different size");
731 return error_mark_node;
733 return build1 (VIEW_CONVERT_EXPR, type, expr);
735 default:
736 error ("aggregate value used where an integer was expected");
737 return convert (type, integer_zero_node);
741 /* Convert EXPR to the complex type TYPE in the usual ways. */
743 tree
744 convert_to_complex (tree type, tree expr)
746 tree subtype = TREE_TYPE (type);
748 switch (TREE_CODE (TREE_TYPE (expr)))
750 case REAL_TYPE:
751 case FIXED_POINT_TYPE:
752 case INTEGER_TYPE:
753 case ENUMERAL_TYPE:
754 case BOOLEAN_TYPE:
755 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
756 convert (subtype, integer_zero_node));
758 case COMPLEX_TYPE:
760 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
762 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
763 return expr;
764 else if (TREE_CODE (expr) == COMPLEX_EXPR)
765 return fold_build2 (COMPLEX_EXPR, type,
766 convert (subtype, TREE_OPERAND (expr, 0)),
767 convert (subtype, TREE_OPERAND (expr, 1)));
768 else
770 expr = save_expr (expr);
771 return
772 fold_build2 (COMPLEX_EXPR, type,
773 convert (subtype,
774 fold_build1 (REALPART_EXPR,
775 TREE_TYPE (TREE_TYPE (expr)),
776 expr)),
777 convert (subtype,
778 fold_build1 (IMAGPART_EXPR,
779 TREE_TYPE (TREE_TYPE (expr)),
780 expr)));
784 case POINTER_TYPE:
785 case REFERENCE_TYPE:
786 error ("pointer value used where a complex was expected");
787 return convert_to_complex (type, integer_zero_node);
789 default:
790 error ("aggregate value used where a complex was expected");
791 return convert_to_complex (type, integer_zero_node);
795 /* Convert EXPR to the vector type TYPE in the usual ways. */
797 tree
798 convert_to_vector (tree type, tree expr)
800 switch (TREE_CODE (TREE_TYPE (expr)))
802 case INTEGER_TYPE:
803 case VECTOR_TYPE:
804 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
806 error ("can't convert between vector values of different size");
807 return error_mark_node;
809 return build1 (VIEW_CONVERT_EXPR, type, expr);
811 default:
812 error ("can't convert value to a vector");
813 return error_mark_node;
817 /* Convert EXPR to some fixed-point type TYPE.
819 EXPR must be fixed-point, float, integer, or enumeral;
820 in other cases error is called. */
822 tree
823 convert_to_fixed (tree type, tree expr)
825 if (integer_zerop (expr))
827 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
828 return fixed_zero_node;
830 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
832 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
833 return fixed_one_node;
836 switch (TREE_CODE (TREE_TYPE (expr)))
838 case FIXED_POINT_TYPE:
839 case INTEGER_TYPE:
840 case ENUMERAL_TYPE:
841 case BOOLEAN_TYPE:
842 case REAL_TYPE:
843 return build1 (FIXED_CONVERT_EXPR, type, expr);
845 case COMPLEX_TYPE:
846 return convert (type,
847 fold_build1 (REALPART_EXPR,
848 TREE_TYPE (TREE_TYPE (expr)), expr));
850 default:
851 error ("aggregate value used where a fixed-point was expected");
852 return error_mark_node;