Daily bump.
[official-gcc.git] / gcc / convert.c
blobf82604e775a5bd1338b341deecdbd84227697e08
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 (TREE_CODE (exp) != NOP_EXPR
102 && TREE_CODE (exp) != CONVERT_EXPR)
103 return exp;
105 sub = TREE_OPERAND (exp, 0);
106 subt = TREE_TYPE (sub);
107 expt = TREE_TYPE (exp);
109 if (!FLOAT_TYPE_P (subt))
110 return exp;
112 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
113 return exp;
115 return strip_float_extensions (sub);
119 /* Convert EXPR to some floating-point type TYPE.
121 EXPR must be float, fixed-point, integer, or enumeral;
122 in other cases error is called. */
124 tree
125 convert_to_real (tree type, tree expr)
127 enum built_in_function fcode = builtin_mathfn_code (expr);
128 tree itype = TREE_TYPE (expr);
130 /* Disable until we figure out how to decide whether the functions are
131 present in runtime. */
132 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
133 if (optimize
134 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
135 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
137 switch (fcode)
139 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
140 CASE_MATHFN (ACOS)
141 CASE_MATHFN (ACOSH)
142 CASE_MATHFN (ASIN)
143 CASE_MATHFN (ASINH)
144 CASE_MATHFN (ATAN)
145 CASE_MATHFN (ATANH)
146 CASE_MATHFN (CBRT)
147 CASE_MATHFN (COS)
148 CASE_MATHFN (COSH)
149 CASE_MATHFN (ERF)
150 CASE_MATHFN (ERFC)
151 CASE_MATHFN (EXP)
152 CASE_MATHFN (EXP10)
153 CASE_MATHFN (EXP2)
154 CASE_MATHFN (EXPM1)
155 CASE_MATHFN (FABS)
156 CASE_MATHFN (GAMMA)
157 CASE_MATHFN (J0)
158 CASE_MATHFN (J1)
159 CASE_MATHFN (LGAMMA)
160 CASE_MATHFN (LOG)
161 CASE_MATHFN (LOG10)
162 CASE_MATHFN (LOG1P)
163 CASE_MATHFN (LOG2)
164 CASE_MATHFN (LOGB)
165 CASE_MATHFN (POW10)
166 CASE_MATHFN (SIN)
167 CASE_MATHFN (SINH)
168 CASE_MATHFN (SQRT)
169 CASE_MATHFN (TAN)
170 CASE_MATHFN (TANH)
171 CASE_MATHFN (TGAMMA)
172 CASE_MATHFN (Y0)
173 CASE_MATHFN (Y1)
174 #undef CASE_MATHFN
176 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
177 tree newtype = type;
179 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
180 the both as the safe type for operation. */
181 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
182 newtype = TREE_TYPE (arg0);
184 /* Be careful about integer to fp conversions.
185 These may overflow still. */
186 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
187 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
188 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
189 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
191 tree fn = mathfn_built_in (newtype, fcode);
193 if (fn)
195 tree arg = fold (convert_to_real (newtype, arg0));
196 expr = build_call_expr (fn, 1, arg);
197 if (newtype == type)
198 return expr;
202 default:
203 break;
206 if (optimize
207 && (((fcode == BUILT_IN_FLOORL
208 || fcode == BUILT_IN_CEILL
209 || fcode == BUILT_IN_ROUNDL
210 || fcode == BUILT_IN_RINTL
211 || fcode == BUILT_IN_TRUNCL
212 || fcode == BUILT_IN_NEARBYINTL)
213 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
214 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
215 || ((fcode == BUILT_IN_FLOOR
216 || fcode == BUILT_IN_CEIL
217 || fcode == BUILT_IN_ROUND
218 || fcode == BUILT_IN_RINT
219 || fcode == BUILT_IN_TRUNC
220 || fcode == BUILT_IN_NEARBYINT)
221 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
223 tree fn = mathfn_built_in (type, fcode);
225 if (fn)
227 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
229 /* Make sure (type)arg0 is an extension, otherwise we could end up
230 changing (float)floor(double d) into floorf((float)d), which is
231 incorrect because (float)d uses round-to-nearest and can round
232 up to the next integer. */
233 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
234 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
238 /* Propagate the cast into the operation. */
239 if (itype != type && FLOAT_TYPE_P (type))
240 switch (TREE_CODE (expr))
242 /* Convert (float)-x into -(float)x. This is safe for
243 round-to-nearest rounding mode. */
244 case ABS_EXPR:
245 case NEGATE_EXPR:
246 if (!flag_rounding_math
247 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
248 return build1 (TREE_CODE (expr), type,
249 fold (convert_to_real (type,
250 TREE_OPERAND (expr, 0))));
251 break;
252 /* Convert (outertype)((innertype0)a+(innertype1)b)
253 into ((newtype)a+(newtype)b) where newtype
254 is the widest mode from all of these. */
255 case PLUS_EXPR:
256 case MINUS_EXPR:
257 case MULT_EXPR:
258 case RDIV_EXPR:
260 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
261 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
263 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
264 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
266 tree newtype = type;
268 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
269 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode)
270 newtype = dfloat32_type_node;
271 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
272 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode)
273 newtype = dfloat64_type_node;
274 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
275 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode)
276 newtype = dfloat128_type_node;
277 if (newtype == dfloat32_type_node
278 || newtype == dfloat64_type_node
279 || newtype == dfloat128_type_node)
281 expr = build2 (TREE_CODE (expr), newtype,
282 fold (convert_to_real (newtype, arg0)),
283 fold (convert_to_real (newtype, arg1)));
284 if (newtype == type)
285 return expr;
286 break;
289 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
290 newtype = TREE_TYPE (arg0);
291 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
292 newtype = TREE_TYPE (arg1);
293 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
295 expr = build2 (TREE_CODE (expr), newtype,
296 fold (convert_to_real (newtype, arg0)),
297 fold (convert_to_real (newtype, arg1)));
298 if (newtype == type)
299 return expr;
303 break;
304 default:
305 break;
308 switch (TREE_CODE (TREE_TYPE (expr)))
310 case REAL_TYPE:
311 /* Ignore the conversion if we don't need to store intermediate
312 results and neither type is a decimal float. */
313 return build1 ((flag_float_store
314 || DECIMAL_FLOAT_TYPE_P (type)
315 || DECIMAL_FLOAT_TYPE_P (itype))
316 ? CONVERT_EXPR : NOP_EXPR, type, expr);
318 case INTEGER_TYPE:
319 case ENUMERAL_TYPE:
320 case BOOLEAN_TYPE:
321 return build1 (FLOAT_EXPR, type, expr);
323 case FIXED_POINT_TYPE:
324 return build1 (FIXED_CONVERT_EXPR, type, expr);
326 case COMPLEX_TYPE:
327 return convert (type,
328 fold_build1 (REALPART_EXPR,
329 TREE_TYPE (TREE_TYPE (expr)), expr));
331 case POINTER_TYPE:
332 case REFERENCE_TYPE:
333 error ("pointer value used where a floating point value was expected");
334 return convert_to_real (type, integer_zero_node);
336 default:
337 error ("aggregate value used where a float was expected");
338 return convert_to_real (type, integer_zero_node);
342 /* Convert EXPR to some integer (or enum) type TYPE.
344 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
345 fixed-point or vector; in other cases error is called.
347 The result of this is always supposed to be a newly created tree node
348 not in use in any existing structure. */
350 tree
351 convert_to_integer (tree type, tree expr)
353 enum tree_code ex_form = TREE_CODE (expr);
354 tree intype = TREE_TYPE (expr);
355 unsigned int inprec = TYPE_PRECISION (intype);
356 unsigned int outprec = TYPE_PRECISION (type);
358 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
359 be. Consider `enum E = { a, b = (enum E) 3 };'. */
360 if (!COMPLETE_TYPE_P (type))
362 error ("conversion to incomplete type");
363 return error_mark_node;
366 /* Convert e.g. (long)round(d) -> lround(d). */
367 /* If we're converting to char, we may encounter differing behavior
368 between converting from double->char vs double->long->char.
369 We're in "undefined" territory but we prefer to be conservative,
370 so only proceed in "unsafe" math mode. */
371 if (optimize
372 && (flag_unsafe_math_optimizations
373 || (long_integer_type_node
374 && outprec >= TYPE_PRECISION (long_integer_type_node))))
376 tree s_expr = strip_float_extensions (expr);
377 tree s_intype = TREE_TYPE (s_expr);
378 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
379 tree fn = 0;
381 switch (fcode)
383 CASE_FLT_FN (BUILT_IN_CEIL):
384 /* Only convert in ISO C99 mode. */
385 if (!TARGET_C99_FUNCTIONS)
386 break;
387 if (outprec < TYPE_PRECISION (long_integer_type_node)
388 || (outprec == TYPE_PRECISION (long_integer_type_node)
389 && !TYPE_UNSIGNED (type)))
390 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
391 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
392 && !TYPE_UNSIGNED (type))
393 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
394 break;
396 CASE_FLT_FN (BUILT_IN_FLOOR):
397 /* Only convert in ISO C99 mode. */
398 if (!TARGET_C99_FUNCTIONS)
399 break;
400 if (outprec < TYPE_PRECISION (long_integer_type_node)
401 || (outprec == TYPE_PRECISION (long_integer_type_node)
402 && !TYPE_UNSIGNED (type)))
403 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
404 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
405 && !TYPE_UNSIGNED (type))
406 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
407 break;
409 CASE_FLT_FN (BUILT_IN_ROUND):
410 if (outprec < TYPE_PRECISION (long_integer_type_node)
411 || (outprec == TYPE_PRECISION (long_integer_type_node)
412 && !TYPE_UNSIGNED (type)))
413 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
414 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
415 && !TYPE_UNSIGNED (type))
416 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
417 break;
419 CASE_FLT_FN (BUILT_IN_NEARBYINT):
420 /* Only convert nearbyint* if we can ignore math exceptions. */
421 if (flag_trapping_math)
422 break;
423 /* ... Fall through ... */
424 CASE_FLT_FN (BUILT_IN_RINT):
425 if (outprec < TYPE_PRECISION (long_integer_type_node)
426 || (outprec == TYPE_PRECISION (long_integer_type_node)
427 && !TYPE_UNSIGNED (type)))
428 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
429 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
430 && !TYPE_UNSIGNED (type))
431 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
432 break;
434 CASE_FLT_FN (BUILT_IN_TRUNC):
435 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
437 default:
438 break;
441 if (fn)
443 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
444 return convert_to_integer (type, newexpr);
448 switch (TREE_CODE (intype))
450 case POINTER_TYPE:
451 case REFERENCE_TYPE:
452 if (integer_zerop (expr))
453 return build_int_cst (type, 0);
455 /* Convert to an unsigned integer of the correct width first,
456 and from there widen/truncate to the required type. */
457 expr = fold_build1 (CONVERT_EXPR,
458 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
459 expr);
460 return fold_convert (type, expr);
462 case INTEGER_TYPE:
463 case ENUMERAL_TYPE:
464 case BOOLEAN_TYPE:
465 /* If this is a logical operation, which just returns 0 or 1, we can
466 change the type of the expression. */
468 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
470 expr = copy_node (expr);
471 TREE_TYPE (expr) = type;
472 return expr;
475 /* If we are widening the type, put in an explicit conversion.
476 Similarly if we are not changing the width. After this, we know
477 we are truncating EXPR. */
479 else if (outprec >= inprec)
481 enum tree_code code;
482 tree tem;
484 /* If the precision of the EXPR's type is K bits and the
485 destination mode has more bits, and the sign is changing,
486 it is not safe to use a NOP_EXPR. For example, suppose
487 that EXPR's type is a 3-bit unsigned integer type, the
488 TYPE is a 3-bit signed integer type, and the machine mode
489 for the types is 8-bit QImode. In that case, the
490 conversion necessitates an explicit sign-extension. In
491 the signed-to-unsigned case the high-order bits have to
492 be cleared. */
493 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
494 && (TYPE_PRECISION (TREE_TYPE (expr))
495 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
496 code = CONVERT_EXPR;
497 else
498 code = NOP_EXPR;
500 tem = fold_unary (code, type, expr);
501 if (tem)
502 return tem;
504 tem = build1 (code, type, expr);
505 TREE_NO_WARNING (tem) = 1;
506 return tem;
509 /* If TYPE is an enumeral type or a type with a precision less
510 than the number of bits in its mode, do the conversion to the
511 type corresponding to its mode, then do a nop conversion
512 to TYPE. */
513 else if (TREE_CODE (type) == ENUMERAL_TYPE
514 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
515 return build1 (NOP_EXPR, type,
516 convert (lang_hooks.types.type_for_mode
517 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
518 expr));
520 /* Here detect when we can distribute the truncation down past some
521 arithmetic. For example, if adding two longs and converting to an
522 int, we can equally well convert both to ints and then add.
523 For the operations handled here, such truncation distribution
524 is always safe.
525 It is desirable in these cases:
526 1) when truncating down to full-word from a larger size
527 2) when truncating takes no work.
528 3) when at least one operand of the arithmetic has been extended
529 (as by C's default conversions). In this case we need two conversions
530 if we do the arithmetic as already requested, so we might as well
531 truncate both and then combine. Perhaps that way we need only one.
533 Note that in general we cannot do the arithmetic in a type
534 shorter than the desired result of conversion, even if the operands
535 are both extended from a shorter type, because they might overflow
536 if combined in that type. The exceptions to this--the times when
537 two narrow values can be combined in their narrow type even to
538 make a wider result--are handled by "shorten" in build_binary_op. */
540 switch (ex_form)
542 case RSHIFT_EXPR:
543 /* We can pass truncation down through right shifting
544 when the shift count is a nonpositive constant. */
545 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
546 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
547 goto trunc1;
548 break;
550 case LSHIFT_EXPR:
551 /* We can pass truncation down through left shifting
552 when the shift count is a nonnegative constant and
553 the target type is unsigned. */
554 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
555 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
556 && TYPE_UNSIGNED (type)
557 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
559 /* If shift count is less than the width of the truncated type,
560 really shift. */
561 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
562 /* In this case, shifting is like multiplication. */
563 goto trunc1;
564 else
566 /* If it is >= that width, result is zero.
567 Handling this with trunc1 would give the wrong result:
568 (int) ((long long) a << 32) is well defined (as 0)
569 but (int) a << 32 is undefined and would get a
570 warning. */
572 tree t = build_int_cst (type, 0);
574 /* If the original expression had side-effects, we must
575 preserve it. */
576 if (TREE_SIDE_EFFECTS (expr))
577 return build2 (COMPOUND_EXPR, type, expr, t);
578 else
579 return t;
582 break;
584 case MAX_EXPR:
585 case MIN_EXPR:
586 case MULT_EXPR:
588 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
589 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
591 /* Don't distribute unless the output precision is at least as big
592 as the actual inputs. Otherwise, the comparison of the
593 truncated values will be wrong. */
594 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
595 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
596 /* If signedness of arg0 and arg1 don't match,
597 we can't necessarily find a type to compare them in. */
598 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
599 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
600 goto trunc1;
601 break;
604 case PLUS_EXPR:
605 case MINUS_EXPR:
606 case BIT_AND_EXPR:
607 case BIT_IOR_EXPR:
608 case BIT_XOR_EXPR:
609 trunc1:
611 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
612 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
614 if (outprec >= BITS_PER_WORD
615 || TRULY_NOOP_TRUNCATION (outprec, inprec)
616 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
617 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
619 /* Do the arithmetic in type TYPEX,
620 then convert result to TYPE. */
621 tree typex = type;
623 /* Can't do arithmetic in enumeral types
624 so use an integer type that will hold the values. */
625 if (TREE_CODE (typex) == ENUMERAL_TYPE)
626 typex = lang_hooks.types.type_for_size
627 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
629 /* But now perhaps TYPEX is as wide as INPREC.
630 In that case, do nothing special here.
631 (Otherwise would recurse infinitely in convert. */
632 if (TYPE_PRECISION (typex) != inprec)
634 /* Don't do unsigned arithmetic where signed was wanted,
635 or vice versa.
636 Exception: if both of the original operands were
637 unsigned then we can safely do the work as unsigned.
638 Exception: shift operations take their type solely
639 from the first argument.
640 Exception: the LSHIFT_EXPR case above requires that
641 we perform this operation unsigned lest we produce
642 signed-overflow undefinedness.
643 And we may need to do it as unsigned
644 if we truncate to the original size. */
645 if (TYPE_UNSIGNED (TREE_TYPE (expr))
646 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
647 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
648 || ex_form == LSHIFT_EXPR
649 || ex_form == RSHIFT_EXPR
650 || ex_form == LROTATE_EXPR
651 || ex_form == RROTATE_EXPR))
652 || ex_form == LSHIFT_EXPR
653 /* If we have !flag_wrapv, and either ARG0 or
654 ARG1 is of a signed type, we have to do
655 PLUS_EXPR or MINUS_EXPR in an unsigned
656 type. Otherwise, we would introduce
657 signed-overflow undefinedness. */
658 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
659 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
660 && (ex_form == PLUS_EXPR
661 || ex_form == MINUS_EXPR)))
662 typex = unsigned_type_for (typex);
663 else
664 typex = signed_type_for (typex);
665 return convert (type,
666 fold_build2 (ex_form, typex,
667 convert (typex, arg0),
668 convert (typex, arg1)));
672 break;
674 case NEGATE_EXPR:
675 case BIT_NOT_EXPR:
676 /* This is not correct for ABS_EXPR,
677 since we must test the sign before truncation. */
679 tree typex;
681 /* Don't do unsigned arithmetic where signed was wanted,
682 or vice versa. */
683 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
684 typex = unsigned_type_for (type);
685 else
686 typex = signed_type_for (type);
687 return convert (type,
688 fold_build1 (ex_form, typex,
689 convert (typex,
690 TREE_OPERAND (expr, 0))));
693 case NOP_EXPR:
694 /* Don't introduce a
695 "can't convert between vector values of different size" error. */
696 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
697 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
698 != GET_MODE_SIZE (TYPE_MODE (type))))
699 break;
700 /* If truncating after truncating, might as well do all at once.
701 If truncating after extending, we may get rid of wasted work. */
702 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
704 case COND_EXPR:
705 /* It is sometimes worthwhile to push the narrowing down through
706 the conditional and never loses. */
707 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
708 convert (type, TREE_OPERAND (expr, 1)),
709 convert (type, TREE_OPERAND (expr, 2)));
711 default:
712 break;
715 return build1 (CONVERT_EXPR, type, expr);
717 case REAL_TYPE:
718 return build1 (FIX_TRUNC_EXPR, type, expr);
720 case FIXED_POINT_TYPE:
721 return build1 (FIXED_CONVERT_EXPR, type, expr);
723 case COMPLEX_TYPE:
724 return convert (type,
725 fold_build1 (REALPART_EXPR,
726 TREE_TYPE (TREE_TYPE (expr)), expr));
728 case VECTOR_TYPE:
729 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
731 error ("can't convert between vector values of different size");
732 return error_mark_node;
734 return build1 (VIEW_CONVERT_EXPR, type, expr);
736 default:
737 error ("aggregate value used where an integer was expected");
738 return convert (type, integer_zero_node);
742 /* Convert EXPR to the complex type TYPE in the usual ways. */
744 tree
745 convert_to_complex (tree type, tree expr)
747 tree subtype = TREE_TYPE (type);
749 switch (TREE_CODE (TREE_TYPE (expr)))
751 case REAL_TYPE:
752 case FIXED_POINT_TYPE:
753 case INTEGER_TYPE:
754 case ENUMERAL_TYPE:
755 case BOOLEAN_TYPE:
756 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
757 convert (subtype, integer_zero_node));
759 case COMPLEX_TYPE:
761 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
763 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
764 return expr;
765 else if (TREE_CODE (expr) == COMPLEX_EXPR)
766 return fold_build2 (COMPLEX_EXPR, type,
767 convert (subtype, TREE_OPERAND (expr, 0)),
768 convert (subtype, TREE_OPERAND (expr, 1)));
769 else
771 expr = save_expr (expr);
772 return
773 fold_build2 (COMPLEX_EXPR, type,
774 convert (subtype,
775 fold_build1 (REALPART_EXPR,
776 TREE_TYPE (TREE_TYPE (expr)),
777 expr)),
778 convert (subtype,
779 fold_build1 (IMAGPART_EXPR,
780 TREE_TYPE (TREE_TYPE (expr)),
781 expr)));
785 case POINTER_TYPE:
786 case REFERENCE_TYPE:
787 error ("pointer value used where a complex was expected");
788 return convert_to_complex (type, integer_zero_node);
790 default:
791 error ("aggregate value used where a complex was expected");
792 return convert_to_complex (type, integer_zero_node);
796 /* Convert EXPR to the vector type TYPE in the usual ways. */
798 tree
799 convert_to_vector (tree type, tree expr)
801 switch (TREE_CODE (TREE_TYPE (expr)))
803 case INTEGER_TYPE:
804 case VECTOR_TYPE:
805 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
807 error ("can't convert between vector values of different size");
808 return error_mark_node;
810 return build1 (VIEW_CONVERT_EXPR, type, expr);
812 default:
813 error ("can't convert value to a vector");
814 return error_mark_node;
818 /* Convert EXPR to some fixed-point type TYPE.
820 EXPR must be fixed-point, float, integer, or enumeral;
821 in other cases error is called. */
823 tree
824 convert_to_fixed (tree type, tree expr)
826 if (integer_zerop (expr))
828 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
829 return fixed_zero_node;
831 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
833 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
834 return fixed_one_node;
837 switch (TREE_CODE (TREE_TYPE (expr)))
839 case FIXED_POINT_TYPE:
840 case INTEGER_TYPE:
841 case ENUMERAL_TYPE:
842 case BOOLEAN_TYPE:
843 case REAL_TYPE:
844 return build1 (FIXED_CONVERT_EXPR, type, expr);
846 case COMPLEX_TYPE:
847 return convert (type,
848 fold_build1 (REALPART_EXPR,
849 TREE_TYPE (TREE_TYPE (expr)), expr));
851 default:
852 error ("aggregate value used where a fixed-point was expected");
853 return error_mark_node;