1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* These routines are somewhat language-independent utility function
22 intended to be called by the language-specific convert () functions. */
26 #include "coretypes.h"
31 #include "diagnostic-core.h"
33 #include "langhooks.h"
35 /* Convert EXPR to some pointer or reference type TYPE.
36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
37 in other cases error is called. */
40 convert_to_pointer (tree type
, tree expr
)
42 location_t loc
= EXPR_LOCATION (expr
);
43 if (TREE_TYPE (expr
) == type
)
46 switch (TREE_CODE (TREE_TYPE (expr
)))
51 /* If the pointers point to different address spaces, conversion needs
52 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
53 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
54 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
57 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
59 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
66 /* If the input precision differs from the target pointer type
67 precision, first convert the input expression to an integer type of
68 the target precision. Some targets, e.g. VMS, need several pointer
69 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
70 unsigned int pprec
= TYPE_PRECISION (type
);
71 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
74 expr
= fold_build1_loc (loc
, NOP_EXPR
,
75 lang_hooks
.types
.type_for_size (pprec
, 0),
79 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
82 error ("cannot convert to a pointer type");
83 return convert_to_pointer (type
, integer_zero_node
);
88 /* Convert EXPR to some floating-point type TYPE.
90 EXPR must be float, fixed-point, integer, or enumeral;
91 in other cases error is called. */
94 convert_to_real (tree type
, tree expr
)
96 enum built_in_function fcode
= builtin_mathfn_code (expr
);
97 tree itype
= TREE_TYPE (expr
);
99 /* Disable until we figure out how to decide whether the functions are
100 present in runtime. */
101 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
103 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
104 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
108 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
123 /* The above functions may set errno differently with float
124 input or output so this transformation is not safe with
150 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
153 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
154 the both as the safe type for operation. */
155 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
156 newtype
= TREE_TYPE (arg0
);
158 /* Be careful about integer to fp conversions.
159 These may overflow still. */
160 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
161 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
162 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
163 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
165 tree fn
= mathfn_built_in (newtype
, fcode
);
169 tree arg
= fold (convert_to_real (newtype
, arg0
));
170 expr
= build_call_expr (fn
, 1, arg
);
181 && (((fcode
== BUILT_IN_FLOORL
182 || fcode
== BUILT_IN_CEILL
183 || fcode
== BUILT_IN_ROUNDL
184 || fcode
== BUILT_IN_RINTL
185 || fcode
== BUILT_IN_TRUNCL
186 || fcode
== BUILT_IN_NEARBYINTL
)
187 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
188 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
189 || ((fcode
== BUILT_IN_FLOOR
190 || fcode
== BUILT_IN_CEIL
191 || fcode
== BUILT_IN_ROUND
192 || fcode
== BUILT_IN_RINT
193 || fcode
== BUILT_IN_TRUNC
194 || fcode
== BUILT_IN_NEARBYINT
)
195 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
197 tree fn
= mathfn_built_in (type
, fcode
);
201 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
203 /* Make sure (type)arg0 is an extension, otherwise we could end up
204 changing (float)floor(double d) into floorf((float)d), which is
205 incorrect because (float)d uses round-to-nearest and can round
206 up to the next integer. */
207 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
208 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
212 /* Propagate the cast into the operation. */
213 if (itype
!= type
&& FLOAT_TYPE_P (type
))
214 switch (TREE_CODE (expr
))
216 /* Convert (float)-x into -(float)x. This is safe for
217 round-to-nearest rounding mode when the inner type is float. */
220 if (!flag_rounding_math
221 && FLOAT_TYPE_P (itype
)
222 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
223 return build1 (TREE_CODE (expr
), type
,
224 fold (convert_to_real (type
,
225 TREE_OPERAND (expr
, 0))));
227 /* Convert (outertype)((innertype0)a+(innertype1)b)
228 into ((newtype)a+(newtype)b) where newtype
229 is the widest mode from all of these. */
235 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
236 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
238 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
239 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
240 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
244 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
245 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
246 || TYPE_MODE (type
) == SDmode
)
247 newtype
= dfloat32_type_node
;
248 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
249 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
250 || TYPE_MODE (type
) == DDmode
)
251 newtype
= dfloat64_type_node
;
252 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
253 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
254 || TYPE_MODE (type
) == TDmode
)
255 newtype
= dfloat128_type_node
;
256 if (newtype
== dfloat32_type_node
257 || newtype
== dfloat64_type_node
258 || newtype
== dfloat128_type_node
)
260 expr
= build2 (TREE_CODE (expr
), newtype
,
261 fold (convert_to_real (newtype
, arg0
)),
262 fold (convert_to_real (newtype
, arg1
)));
268 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
269 newtype
= TREE_TYPE (arg0
);
270 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
271 newtype
= TREE_TYPE (arg1
);
272 /* Sometimes this transformation is safe (cannot
273 change results through affecting double rounding
274 cases) and sometimes it is not. If NEWTYPE is
275 wider than TYPE, e.g. (float)((long double)double
276 + (long double)double) converted to
277 (float)(double + double), the transformation is
278 unsafe regardless of the details of the types
279 involved; double rounding can arise if the result
280 of NEWTYPE arithmetic is a NEWTYPE value half way
281 between two representable TYPE values but the
282 exact value is sufficiently different (in the
283 right direction) for this difference to be
284 visible in ITYPE arithmetic. If NEWTYPE is the
285 same as TYPE, however, the transformation may be
286 safe depending on the types involved: it is safe
287 if the ITYPE has strictly more than twice as many
288 mantissa bits as TYPE, can represent infinities
289 and NaNs if the TYPE can, and has sufficient
290 exponent range for the product or ratio of two
291 values representable in the TYPE to be within the
292 range of normal values of ITYPE. */
293 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
294 && (flag_unsafe_math_optimizations
295 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
296 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
298 && !excess_precision_type (newtype
))))
300 expr
= build2 (TREE_CODE (expr
), newtype
,
301 fold (convert_to_real (newtype
, arg0
)),
302 fold (convert_to_real (newtype
, arg1
)));
313 switch (TREE_CODE (TREE_TYPE (expr
)))
316 /* Ignore the conversion if we don't need to store intermediate
317 results and neither type is a decimal float. */
318 return build1 ((flag_float_store
319 || DECIMAL_FLOAT_TYPE_P (type
)
320 || DECIMAL_FLOAT_TYPE_P (itype
))
321 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
326 return build1 (FLOAT_EXPR
, type
, expr
);
328 case FIXED_POINT_TYPE
:
329 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
332 return convert (type
,
333 fold_build1 (REALPART_EXPR
,
334 TREE_TYPE (TREE_TYPE (expr
)), expr
));
338 error ("pointer value used where a floating point value was expected");
339 return convert_to_real (type
, integer_zero_node
);
342 error ("aggregate value used where a float was expected");
343 return convert_to_real (type
, integer_zero_node
);
347 /* Convert EXPR to some integer (or enum) type TYPE.
349 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
350 fixed-point or vector; in other cases error is called.
352 The result of this is always supposed to be a newly created tree node
353 not in use in any existing structure. */
356 convert_to_integer (tree type
, tree expr
)
358 enum tree_code ex_form
= TREE_CODE (expr
);
359 tree intype
= TREE_TYPE (expr
);
360 unsigned int inprec
= element_precision (intype
);
361 unsigned int outprec
= element_precision (type
);
363 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
364 be. Consider `enum E = { a, b = (enum E) 3 };'. */
365 if (!COMPLETE_TYPE_P (type
))
367 error ("conversion to incomplete type");
368 return error_mark_node
;
371 /* Convert e.g. (long)round(d) -> lround(d). */
372 /* If we're converting to char, we may encounter differing behavior
373 between converting from double->char vs double->long->char.
374 We're in "undefined" territory but we prefer to be conservative,
375 so only proceed in "unsafe" math mode. */
377 && (flag_unsafe_math_optimizations
378 || (long_integer_type_node
379 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
381 tree s_expr
= strip_float_extensions (expr
);
382 tree s_intype
= TREE_TYPE (s_expr
);
383 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
388 CASE_FLT_FN (BUILT_IN_CEIL
):
389 /* Only convert in ISO C99 mode. */
390 if (!targetm
.libc_has_function (function_c99_misc
))
392 if (outprec
< TYPE_PRECISION (integer_type_node
)
393 || (outprec
== TYPE_PRECISION (integer_type_node
)
394 && !TYPE_UNSIGNED (type
)))
395 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
396 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
397 && !TYPE_UNSIGNED (type
))
398 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
399 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
400 && !TYPE_UNSIGNED (type
))
401 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
404 CASE_FLT_FN (BUILT_IN_FLOOR
):
405 /* Only convert in ISO C99 mode. */
406 if (!targetm
.libc_has_function (function_c99_misc
))
408 if (outprec
< TYPE_PRECISION (integer_type_node
)
409 || (outprec
== TYPE_PRECISION (integer_type_node
)
410 && !TYPE_UNSIGNED (type
)))
411 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
412 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
413 && !TYPE_UNSIGNED (type
))
414 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
415 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
416 && !TYPE_UNSIGNED (type
))
417 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
420 CASE_FLT_FN (BUILT_IN_ROUND
):
421 /* Only convert in ISO C99 mode. */
422 if (!targetm
.libc_has_function (function_c99_misc
))
424 if (outprec
< TYPE_PRECISION (integer_type_node
)
425 || (outprec
== TYPE_PRECISION (integer_type_node
)
426 && !TYPE_UNSIGNED (type
)))
427 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
428 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
429 && !TYPE_UNSIGNED (type
))
430 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
431 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
432 && !TYPE_UNSIGNED (type
))
433 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
436 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
437 /* Only convert nearbyint* if we can ignore math exceptions. */
438 if (flag_trapping_math
)
440 /* ... Fall through ... */
441 CASE_FLT_FN (BUILT_IN_RINT
):
442 /* Only convert in ISO C99 mode. */
443 if (!targetm
.libc_has_function (function_c99_misc
))
445 if (outprec
< TYPE_PRECISION (integer_type_node
)
446 || (outprec
== TYPE_PRECISION (integer_type_node
)
447 && !TYPE_UNSIGNED (type
)))
448 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
449 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
450 && !TYPE_UNSIGNED (type
))
451 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
452 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
453 && !TYPE_UNSIGNED (type
))
454 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
457 CASE_FLT_FN (BUILT_IN_TRUNC
):
458 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
466 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
467 return convert_to_integer (type
, newexpr
);
471 /* Convert (int)logb(d) -> ilogb(d). */
473 && flag_unsafe_math_optimizations
474 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
476 && (outprec
> TYPE_PRECISION (integer_type_node
)
477 || (outprec
== TYPE_PRECISION (integer_type_node
)
478 && !TYPE_UNSIGNED (type
))))
480 tree s_expr
= strip_float_extensions (expr
);
481 tree s_intype
= TREE_TYPE (s_expr
);
482 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
487 CASE_FLT_FN (BUILT_IN_LOGB
):
488 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
497 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
498 return convert_to_integer (type
, newexpr
);
502 switch (TREE_CODE (intype
))
506 if (integer_zerop (expr
))
507 return build_int_cst (type
, 0);
509 /* Convert to an unsigned integer of the correct width first, and from
510 there widen/truncate to the required type. Some targets support the
511 coexistence of multiple valid pointer sizes, so fetch the one we need
513 expr
= fold_build1 (CONVERT_EXPR
,
514 lang_hooks
.types
.type_for_size
515 (TYPE_PRECISION (intype
), 0),
517 return fold_convert (type
, expr
);
523 /* If this is a logical operation, which just returns 0 or 1, we can
524 change the type of the expression. */
526 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
528 expr
= copy_node (expr
);
529 TREE_TYPE (expr
) = type
;
533 /* If we are widening the type, put in an explicit conversion.
534 Similarly if we are not changing the width. After this, we know
535 we are truncating EXPR. */
537 else if (outprec
>= inprec
)
541 /* If the precision of the EXPR's type is K bits and the
542 destination mode has more bits, and the sign is changing,
543 it is not safe to use a NOP_EXPR. For example, suppose
544 that EXPR's type is a 3-bit unsigned integer type, the
545 TYPE is a 3-bit signed integer type, and the machine mode
546 for the types is 8-bit QImode. In that case, the
547 conversion necessitates an explicit sign-extension. In
548 the signed-to-unsigned case the high-order bits have to
550 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
551 && (TYPE_PRECISION (TREE_TYPE (expr
))
552 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
557 return fold_build1 (code
, type
, expr
);
560 /* If TYPE is an enumeral type or a type with a precision less
561 than the number of bits in its mode, do the conversion to the
562 type corresponding to its mode, then do a nop conversion
564 else if (TREE_CODE (type
) == ENUMERAL_TYPE
565 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
566 return build1 (NOP_EXPR
, type
,
567 convert (lang_hooks
.types
.type_for_mode
568 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
571 /* Here detect when we can distribute the truncation down past some
572 arithmetic. For example, if adding two longs and converting to an
573 int, we can equally well convert both to ints and then add.
574 For the operations handled here, such truncation distribution
576 It is desirable in these cases:
577 1) when truncating down to full-word from a larger size
578 2) when truncating takes no work.
579 3) when at least one operand of the arithmetic has been extended
580 (as by C's default conversions). In this case we need two conversions
581 if we do the arithmetic as already requested, so we might as well
582 truncate both and then combine. Perhaps that way we need only one.
584 Note that in general we cannot do the arithmetic in a type
585 shorter than the desired result of conversion, even if the operands
586 are both extended from a shorter type, because they might overflow
587 if combined in that type. The exceptions to this--the times when
588 two narrow values can be combined in their narrow type even to
589 make a wider result--are handled by "shorten" in build_binary_op. */
594 /* We can pass truncation down through right shifting
595 when the shift count is a nonpositive constant. */
596 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
597 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
602 /* We can pass truncation down through left shifting
603 when the shift count is a nonnegative constant and
604 the target type is unsigned. */
605 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
606 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
607 && TYPE_UNSIGNED (type
)
608 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
610 /* If shift count is less than the width of the truncated type,
612 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
613 /* In this case, shifting is like multiplication. */
617 /* If it is >= that width, result is zero.
618 Handling this with trunc1 would give the wrong result:
619 (int) ((long long) a << 32) is well defined (as 0)
620 but (int) a << 32 is undefined and would get a
623 tree t
= build_int_cst (type
, 0);
625 /* If the original expression had side-effects, we must
627 if (TREE_SIDE_EFFECTS (expr
))
628 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
637 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
638 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
640 /* Don't distribute unless the output precision is at least as big
641 as the actual inputs and it has the same signedness. */
642 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
643 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
644 /* If signedness of arg0 and arg1 don't match,
645 we can't necessarily find a type to compare them in. */
646 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
647 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
648 /* Do not change the sign of the division. */
649 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
650 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
651 /* Either require unsigned division or a division by
652 a constant that is not -1. */
653 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
654 || (TREE_CODE (arg1
) == INTEGER_CST
655 && !integer_all_onesp (arg1
))))
664 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
665 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
667 /* Don't distribute unless the output precision is at least as big
668 as the actual inputs. Otherwise, the comparison of the
669 truncated values will be wrong. */
670 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
671 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
672 /* If signedness of arg0 and arg1 don't match,
673 we can't necessarily find a type to compare them in. */
674 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
675 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
687 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
688 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
690 /* Do not try to narrow operands of pointer subtraction;
691 that will interfere with other folding. */
692 if (ex_form
== MINUS_EXPR
693 && CONVERT_EXPR_P (arg0
)
694 && CONVERT_EXPR_P (arg1
)
695 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
696 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
699 if (outprec
>= BITS_PER_WORD
700 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
701 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
702 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
704 /* Do the arithmetic in type TYPEX,
705 then convert result to TYPE. */
708 /* Can't do arithmetic in enumeral types
709 so use an integer type that will hold the values. */
710 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
711 typex
= lang_hooks
.types
.type_for_size
712 (TYPE_PRECISION (typex
), TYPE_UNSIGNED (typex
));
714 /* But now perhaps TYPEX is as wide as INPREC.
715 In that case, do nothing special here.
716 (Otherwise would recurse infinitely in convert. */
717 if (TYPE_PRECISION (typex
) != inprec
)
719 /* Don't do unsigned arithmetic where signed was wanted,
721 Exception: if both of the original operands were
722 unsigned then we can safely do the work as unsigned.
723 Exception: shift operations take their type solely
724 from the first argument.
725 Exception: the LSHIFT_EXPR case above requires that
726 we perform this operation unsigned lest we produce
727 signed-overflow undefinedness.
728 And we may need to do it as unsigned
729 if we truncate to the original size. */
730 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
731 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
732 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
733 || ex_form
== LSHIFT_EXPR
734 || ex_form
== RSHIFT_EXPR
735 || ex_form
== LROTATE_EXPR
736 || ex_form
== RROTATE_EXPR
))
737 || ex_form
== LSHIFT_EXPR
738 /* If we have !flag_wrapv, and either ARG0 or
739 ARG1 is of a signed type, we have to do
740 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
741 type in case the operation in outprec precision
742 could overflow. Otherwise, we would introduce
743 signed-overflow undefinedness. */
744 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
745 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
746 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
748 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
750 && (ex_form
== PLUS_EXPR
751 || ex_form
== MINUS_EXPR
752 || ex_form
== MULT_EXPR
)))
753 typex
= unsigned_type_for (typex
);
755 typex
= signed_type_for (typex
);
756 return convert (type
,
757 fold_build2 (ex_form
, typex
,
758 convert (typex
, arg0
),
759 convert (typex
, arg1
)));
767 /* This is not correct for ABS_EXPR,
768 since we must test the sign before truncation. */
770 tree typex
= unsigned_type_for (type
);
771 return convert (type
,
772 fold_build1 (ex_form
, typex
,
774 TREE_OPERAND (expr
, 0))));
779 "can't convert between vector values of different size" error. */
780 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
781 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
782 != GET_MODE_SIZE (TYPE_MODE (type
))))
784 /* If truncating after truncating, might as well do all at once.
785 If truncating after extending, we may get rid of wasted work. */
786 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
789 /* It is sometimes worthwhile to push the narrowing down through
790 the conditional and never loses. A COND_EXPR may have a throw
791 as one operand, which then has void type. Just leave void
792 operands as they are. */
793 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
794 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
795 ? TREE_OPERAND (expr
, 1)
796 : convert (type
, TREE_OPERAND (expr
, 1)),
797 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
798 ? TREE_OPERAND (expr
, 2)
799 : convert (type
, TREE_OPERAND (expr
, 2)));
805 /* When parsing long initializers, we might end up with a lot of casts.
807 if (TREE_CODE (expr
) == INTEGER_CST
)
808 return fold_convert (type
, expr
);
809 return build1 (CONVERT_EXPR
, type
, expr
);
812 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
814 case FIXED_POINT_TYPE
:
815 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
818 return convert (type
,
819 fold_build1 (REALPART_EXPR
,
820 TREE_TYPE (TREE_TYPE (expr
)), expr
));
823 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
825 error ("can%'t convert between vector values of different size");
826 return error_mark_node
;
828 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
831 error ("aggregate value used where an integer was expected");
832 return convert (type
, integer_zero_node
);
836 /* Convert EXPR to the complex type TYPE in the usual ways. */
839 convert_to_complex (tree type
, tree expr
)
841 tree subtype
= TREE_TYPE (type
);
843 switch (TREE_CODE (TREE_TYPE (expr
)))
846 case FIXED_POINT_TYPE
:
850 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
851 convert (subtype
, integer_zero_node
));
855 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
857 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
859 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
860 return fold_build2 (COMPLEX_EXPR
, type
,
861 convert (subtype
, TREE_OPERAND (expr
, 0)),
862 convert (subtype
, TREE_OPERAND (expr
, 1)));
865 expr
= save_expr (expr
);
867 fold_build2 (COMPLEX_EXPR
, type
,
869 fold_build1 (REALPART_EXPR
,
870 TREE_TYPE (TREE_TYPE (expr
)),
873 fold_build1 (IMAGPART_EXPR
,
874 TREE_TYPE (TREE_TYPE (expr
)),
881 error ("pointer value used where a complex was expected");
882 return convert_to_complex (type
, integer_zero_node
);
885 error ("aggregate value used where a complex was expected");
886 return convert_to_complex (type
, integer_zero_node
);
890 /* Convert EXPR to the vector type TYPE in the usual ways. */
893 convert_to_vector (tree type
, tree expr
)
895 switch (TREE_CODE (TREE_TYPE (expr
)))
899 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
901 error ("can%'t convert between vector values of different size");
902 return error_mark_node
;
904 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
907 error ("can%'t convert value to a vector");
908 return error_mark_node
;
912 /* Convert EXPR to some fixed-point type TYPE.
914 EXPR must be fixed-point, float, integer, or enumeral;
915 in other cases error is called. */
918 convert_to_fixed (tree type
, tree expr
)
920 if (integer_zerop (expr
))
922 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
923 return fixed_zero_node
;
925 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
927 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
928 return fixed_one_node
;
931 switch (TREE_CODE (TREE_TYPE (expr
)))
933 case FIXED_POINT_TYPE
:
938 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
941 return convert (type
,
942 fold_build1 (REALPART_EXPR
,
943 TREE_TYPE (TREE_TYPE (expr
)), expr
));
946 error ("aggregate value used where a fixed-point was expected");
947 return error_mark_node
;