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"
32 #include "langhooks.h"
34 /* Convert EXPR to some pointer or reference type TYPE.
35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
36 in other cases error is called. */
39 convert_to_pointer (tree type
, tree expr
)
41 location_t loc
= EXPR_LOCATION (expr
);
42 if (TREE_TYPE (expr
) == type
)
45 switch (TREE_CODE (TREE_TYPE (expr
)))
50 /* If the pointers point to different address spaces, conversion needs
51 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
52 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
53 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
56 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
58 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
65 /* If the input precision differs from the target pointer type
66 precision, first convert the input expression to an integer type of
67 the target precision. Some targets, e.g. VMS, need several pointer
68 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
69 unsigned int pprec
= TYPE_PRECISION (type
);
70 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
73 expr
= fold_build1_loc (loc
, NOP_EXPR
,
74 lang_hooks
.types
.type_for_size (pprec
, 0),
78 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
81 error ("cannot convert to a pointer type");
82 return convert_to_pointer (type
, integer_zero_node
);
87 /* Convert EXPR to some floating-point type TYPE.
89 EXPR must be float, fixed-point, integer, or enumeral;
90 in other cases error is called. */
93 convert_to_real (tree type
, tree expr
)
95 enum built_in_function fcode
= builtin_mathfn_code (expr
);
96 tree itype
= TREE_TYPE (expr
);
98 /* Disable until we figure out how to decide whether the functions are
99 present in runtime. */
100 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
102 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
103 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
107 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
122 /* The above functions may set errno differently with float
123 input or output so this transformation is not safe with
149 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
152 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
153 the both as the safe type for operation. */
154 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
155 newtype
= TREE_TYPE (arg0
);
157 /* Be careful about integer to fp conversions.
158 These may overflow still. */
159 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
160 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
161 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
162 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
164 tree fn
= mathfn_built_in (newtype
, fcode
);
168 tree arg
= fold (convert_to_real (newtype
, arg0
));
169 expr
= build_call_expr (fn
, 1, arg
);
180 && (((fcode
== BUILT_IN_FLOORL
181 || fcode
== BUILT_IN_CEILL
182 || fcode
== BUILT_IN_ROUNDL
183 || fcode
== BUILT_IN_RINTL
184 || fcode
== BUILT_IN_TRUNCL
185 || fcode
== BUILT_IN_NEARBYINTL
)
186 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
187 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
188 || ((fcode
== BUILT_IN_FLOOR
189 || fcode
== BUILT_IN_CEIL
190 || fcode
== BUILT_IN_ROUND
191 || fcode
== BUILT_IN_RINT
192 || fcode
== BUILT_IN_TRUNC
193 || fcode
== BUILT_IN_NEARBYINT
)
194 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
196 tree fn
= mathfn_built_in (type
, fcode
);
200 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
202 /* Make sure (type)arg0 is an extension, otherwise we could end up
203 changing (float)floor(double d) into floorf((float)d), which is
204 incorrect because (float)d uses round-to-nearest and can round
205 up to the next integer. */
206 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
207 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
211 /* Propagate the cast into the operation. */
212 if (itype
!= type
&& FLOAT_TYPE_P (type
))
213 switch (TREE_CODE (expr
))
215 /* Convert (float)-x into -(float)x. This is safe for
216 round-to-nearest rounding mode. */
219 if (!flag_rounding_math
220 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (expr
)))
221 return build1 (TREE_CODE (expr
), type
,
222 fold (convert_to_real (type
,
223 TREE_OPERAND (expr
, 0))));
225 /* Convert (outertype)((innertype0)a+(innertype1)b)
226 into ((newtype)a+(newtype)b) where newtype
227 is the widest mode from all of these. */
233 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
234 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
236 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
237 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
238 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
242 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
243 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
244 || TYPE_MODE (type
) == SDmode
)
245 newtype
= dfloat32_type_node
;
246 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
247 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
248 || TYPE_MODE (type
) == DDmode
)
249 newtype
= dfloat64_type_node
;
250 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
251 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
252 || TYPE_MODE (type
) == TDmode
)
253 newtype
= dfloat128_type_node
;
254 if (newtype
== dfloat32_type_node
255 || newtype
== dfloat64_type_node
256 || newtype
== dfloat128_type_node
)
258 expr
= build2 (TREE_CODE (expr
), newtype
,
259 fold (convert_to_real (newtype
, arg0
)),
260 fold (convert_to_real (newtype
, arg1
)));
266 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
267 newtype
= TREE_TYPE (arg0
);
268 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
269 newtype
= TREE_TYPE (arg1
);
270 /* Sometimes this transformation is safe (cannot
271 change results through affecting double rounding
272 cases) and sometimes it is not. If NEWTYPE is
273 wider than TYPE, e.g. (float)((long double)double
274 + (long double)double) converted to
275 (float)(double + double), the transformation is
276 unsafe regardless of the details of the types
277 involved; double rounding can arise if the result
278 of NEWTYPE arithmetic is a NEWTYPE value half way
279 between two representable TYPE values but the
280 exact value is sufficiently different (in the
281 right direction) for this difference to be
282 visible in ITYPE arithmetic. If NEWTYPE is the
283 same as TYPE, however, the transformation may be
284 safe depending on the types involved: it is safe
285 if the ITYPE has strictly more than twice as many
286 mantissa bits as TYPE, can represent infinities
287 and NaNs if the TYPE can, and has sufficient
288 exponent range for the product or ratio of two
289 values representable in the TYPE to be within the
290 range of normal values of ITYPE. */
291 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
292 && (flag_unsafe_math_optimizations
293 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
294 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
296 && !excess_precision_type (newtype
))))
298 expr
= build2 (TREE_CODE (expr
), newtype
,
299 fold (convert_to_real (newtype
, arg0
)),
300 fold (convert_to_real (newtype
, arg1
)));
311 switch (TREE_CODE (TREE_TYPE (expr
)))
314 /* Ignore the conversion if we don't need to store intermediate
315 results and neither type is a decimal float. */
316 return build1 ((flag_float_store
317 || DECIMAL_FLOAT_TYPE_P (type
)
318 || DECIMAL_FLOAT_TYPE_P (itype
))
319 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
324 return build1 (FLOAT_EXPR
, type
, expr
);
326 case FIXED_POINT_TYPE
:
327 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
330 return convert (type
,
331 fold_build1 (REALPART_EXPR
,
332 TREE_TYPE (TREE_TYPE (expr
)), expr
));
336 error ("pointer value used where a floating point value was expected");
337 return convert_to_real (type
, integer_zero_node
);
340 error ("aggregate value used where a float was expected");
341 return convert_to_real (type
, integer_zero_node
);
345 /* Convert EXPR to some integer (or enum) type TYPE.
347 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
348 fixed-point or vector; in other cases error is called.
350 The result of this is always supposed to be a newly created tree node
351 not in use in any existing structure. */
354 convert_to_integer (tree type
, tree expr
)
356 enum tree_code ex_form
= TREE_CODE (expr
);
357 tree intype
= TREE_TYPE (expr
);
358 unsigned int inprec
= TYPE_PRECISION (intype
);
359 unsigned int outprec
= TYPE_PRECISION (type
);
361 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
362 be. Consider `enum E = { a, b = (enum E) 3 };'. */
363 if (!COMPLETE_TYPE_P (type
))
365 error ("conversion to incomplete type");
366 return error_mark_node
;
369 /* Convert e.g. (long)round(d) -> lround(d). */
370 /* If we're converting to char, we may encounter differing behavior
371 between converting from double->char vs double->long->char.
372 We're in "undefined" territory but we prefer to be conservative,
373 so only proceed in "unsafe" math mode. */
375 && (flag_unsafe_math_optimizations
376 || (long_integer_type_node
377 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
379 tree s_expr
= strip_float_extensions (expr
);
380 tree s_intype
= TREE_TYPE (s_expr
);
381 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
386 CASE_FLT_FN (BUILT_IN_CEIL
):
387 /* Only convert in ISO C99 mode. */
388 if (!TARGET_C99_FUNCTIONS
)
390 if (outprec
< TYPE_PRECISION (integer_type_node
)
391 || (outprec
== TYPE_PRECISION (integer_type_node
)
392 && !TYPE_UNSIGNED (type
)))
393 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
394 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
395 && !TYPE_UNSIGNED (type
))
396 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
397 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
398 && !TYPE_UNSIGNED (type
))
399 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
402 CASE_FLT_FN (BUILT_IN_FLOOR
):
403 /* Only convert in ISO C99 mode. */
404 if (!TARGET_C99_FUNCTIONS
)
406 if (outprec
< TYPE_PRECISION (integer_type_node
)
407 || (outprec
== TYPE_PRECISION (integer_type_node
)
408 && !TYPE_UNSIGNED (type
)))
409 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
410 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
411 && !TYPE_UNSIGNED (type
))
412 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
413 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
414 && !TYPE_UNSIGNED (type
))
415 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
418 CASE_FLT_FN (BUILT_IN_ROUND
):
419 /* Only convert in ISO C99 mode. */
420 if (!TARGET_C99_FUNCTIONS
)
422 if (outprec
< TYPE_PRECISION (integer_type_node
)
423 || (outprec
== TYPE_PRECISION (integer_type_node
)
424 && !TYPE_UNSIGNED (type
)))
425 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
426 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
427 && !TYPE_UNSIGNED (type
))
428 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
429 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
430 && !TYPE_UNSIGNED (type
))
431 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
434 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
435 /* Only convert nearbyint* if we can ignore math exceptions. */
436 if (flag_trapping_math
)
438 /* ... Fall through ... */
439 CASE_FLT_FN (BUILT_IN_RINT
):
440 /* Only convert in ISO C99 mode. */
441 if (!TARGET_C99_FUNCTIONS
)
443 if (outprec
< TYPE_PRECISION (integer_type_node
)
444 || (outprec
== TYPE_PRECISION (integer_type_node
)
445 && !TYPE_UNSIGNED (type
)))
446 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
447 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
448 && !TYPE_UNSIGNED (type
))
449 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
450 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
451 && !TYPE_UNSIGNED (type
))
452 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
455 CASE_FLT_FN (BUILT_IN_TRUNC
):
456 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
464 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
465 return convert_to_integer (type
, newexpr
);
469 /* Convert (int)logb(d) -> ilogb(d). */
471 && flag_unsafe_math_optimizations
472 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
474 && (outprec
> TYPE_PRECISION (integer_type_node
)
475 || (outprec
== TYPE_PRECISION (integer_type_node
)
476 && !TYPE_UNSIGNED (type
))))
478 tree s_expr
= strip_float_extensions (expr
);
479 tree s_intype
= TREE_TYPE (s_expr
);
480 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
485 CASE_FLT_FN (BUILT_IN_LOGB
):
486 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
495 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
496 return convert_to_integer (type
, newexpr
);
500 switch (TREE_CODE (intype
))
504 if (integer_zerop (expr
))
505 return build_int_cst (type
, 0);
507 /* Convert to an unsigned integer of the correct width first, and from
508 there widen/truncate to the required type. Some targets support the
509 coexistence of multiple valid pointer sizes, so fetch the one we need
511 expr
= fold_build1 (CONVERT_EXPR
,
512 lang_hooks
.types
.type_for_size
513 (TYPE_PRECISION (intype
), 0),
515 return fold_convert (type
, expr
);
521 /* If this is a logical operation, which just returns 0 or 1, we can
522 change the type of the expression. */
524 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
526 expr
= copy_node (expr
);
527 TREE_TYPE (expr
) = type
;
531 /* If we are widening the type, put in an explicit conversion.
532 Similarly if we are not changing the width. After this, we know
533 we are truncating EXPR. */
535 else if (outprec
>= inprec
)
539 /* If the precision of the EXPR's type is K bits and the
540 destination mode has more bits, and the sign is changing,
541 it is not safe to use a NOP_EXPR. For example, suppose
542 that EXPR's type is a 3-bit unsigned integer type, the
543 TYPE is a 3-bit signed integer type, and the machine mode
544 for the types is 8-bit QImode. In that case, the
545 conversion necessitates an explicit sign-extension. In
546 the signed-to-unsigned case the high-order bits have to
548 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
549 && (TYPE_PRECISION (TREE_TYPE (expr
))
550 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
555 return fold_build1 (code
, type
, expr
);
558 /* If TYPE is an enumeral type or a type with a precision less
559 than the number of bits in its mode, do the conversion to the
560 type corresponding to its mode, then do a nop conversion
562 else if (TREE_CODE (type
) == ENUMERAL_TYPE
563 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
564 return build1 (NOP_EXPR
, type
,
565 convert (lang_hooks
.types
.type_for_mode
566 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
569 /* Here detect when we can distribute the truncation down past some
570 arithmetic. For example, if adding two longs and converting to an
571 int, we can equally well convert both to ints and then add.
572 For the operations handled here, such truncation distribution
574 It is desirable in these cases:
575 1) when truncating down to full-word from a larger size
576 2) when truncating takes no work.
577 3) when at least one operand of the arithmetic has been extended
578 (as by C's default conversions). In this case we need two conversions
579 if we do the arithmetic as already requested, so we might as well
580 truncate both and then combine. Perhaps that way we need only one.
582 Note that in general we cannot do the arithmetic in a type
583 shorter than the desired result of conversion, even if the operands
584 are both extended from a shorter type, because they might overflow
585 if combined in that type. The exceptions to this--the times when
586 two narrow values can be combined in their narrow type even to
587 make a wider result--are handled by "shorten" in build_binary_op. */
592 /* We can pass truncation down through right shifting
593 when the shift count is a nonpositive constant. */
594 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
595 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
600 /* We can pass truncation down through left shifting
601 when the shift count is a nonnegative constant and
602 the target type is unsigned. */
603 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
604 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
605 && TYPE_UNSIGNED (type
)
606 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
608 /* If shift count is less than the width of the truncated type,
610 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
611 /* In this case, shifting is like multiplication. */
615 /* If it is >= that width, result is zero.
616 Handling this with trunc1 would give the wrong result:
617 (int) ((long long) a << 32) is well defined (as 0)
618 but (int) a << 32 is undefined and would get a
621 tree t
= build_int_cst (type
, 0);
623 /* If the original expression had side-effects, we must
625 if (TREE_SIDE_EFFECTS (expr
))
626 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
635 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
636 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
638 /* Don't distribute unless the output precision is at least as big
639 as the actual inputs and it has the same signedness. */
640 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
641 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
642 /* If signedness of arg0 and arg1 don't match,
643 we can't necessarily find a type to compare them in. */
644 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
645 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
646 /* Do not change the sign of the division. */
647 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
648 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
649 /* Either require unsigned division or a division by
650 a constant that is not -1. */
651 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
652 || (TREE_CODE (arg1
) == INTEGER_CST
653 && !integer_all_onesp (arg1
))))
662 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
663 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
665 /* Don't distribute unless the output precision is at least as big
666 as the actual inputs. Otherwise, the comparison of the
667 truncated values will be wrong. */
668 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
669 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
670 /* If signedness of arg0 and arg1 don't match,
671 we can't necessarily find a type to compare them in. */
672 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
673 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
685 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
686 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
688 /* Do not try to narrow operands of pointer subtraction;
689 that will interfere with other folding. */
690 if (ex_form
== MINUS_EXPR
691 && CONVERT_EXPR_P (arg0
)
692 && CONVERT_EXPR_P (arg1
)
693 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
694 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
697 if (outprec
>= BITS_PER_WORD
698 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
699 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
700 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
702 /* Do the arithmetic in type TYPEX,
703 then convert result to TYPE. */
706 /* Can't do arithmetic in enumeral types
707 so use an integer type that will hold the values. */
708 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
709 typex
= lang_hooks
.types
.type_for_size
710 (TYPE_PRECISION (typex
), TYPE_UNSIGNED (typex
));
712 /* But now perhaps TYPEX is as wide as INPREC.
713 In that case, do nothing special here.
714 (Otherwise would recurse infinitely in convert. */
715 if (TYPE_PRECISION (typex
) != inprec
)
717 /* Don't do unsigned arithmetic where signed was wanted,
719 Exception: if both of the original operands were
720 unsigned then we can safely do the work as unsigned.
721 Exception: shift operations take their type solely
722 from the first argument.
723 Exception: the LSHIFT_EXPR case above requires that
724 we perform this operation unsigned lest we produce
725 signed-overflow undefinedness.
726 And we may need to do it as unsigned
727 if we truncate to the original size. */
728 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
729 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
730 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
731 || ex_form
== LSHIFT_EXPR
732 || ex_form
== RSHIFT_EXPR
733 || ex_form
== LROTATE_EXPR
734 || ex_form
== RROTATE_EXPR
))
735 || ex_form
== LSHIFT_EXPR
736 /* If we have !flag_wrapv, and either ARG0 or
737 ARG1 is of a signed type, we have to do
738 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
739 type in case the operation in outprec precision
740 could overflow. Otherwise, we would introduce
741 signed-overflow undefinedness. */
742 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
743 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
744 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
746 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
748 && (ex_form
== PLUS_EXPR
749 || ex_form
== MINUS_EXPR
750 || ex_form
== MULT_EXPR
)))
751 typex
= unsigned_type_for (typex
);
753 typex
= signed_type_for (typex
);
754 return convert (type
,
755 fold_build2 (ex_form
, typex
,
756 convert (typex
, arg0
),
757 convert (typex
, arg1
)));
765 /* This is not correct for ABS_EXPR,
766 since we must test the sign before truncation. */
768 tree typex
= unsigned_type_for (type
);
769 return convert (type
,
770 fold_build1 (ex_form
, typex
,
772 TREE_OPERAND (expr
, 0))));
777 "can't convert between vector values of different size" error. */
778 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
779 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
780 != GET_MODE_SIZE (TYPE_MODE (type
))))
782 /* If truncating after truncating, might as well do all at once.
783 If truncating after extending, we may get rid of wasted work. */
784 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
787 /* It is sometimes worthwhile to push the narrowing down through
788 the conditional and never loses. A COND_EXPR may have a throw
789 as one operand, which then has void type. Just leave void
790 operands as they are. */
791 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
792 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
793 ? TREE_OPERAND (expr
, 1)
794 : convert (type
, TREE_OPERAND (expr
, 1)),
795 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
796 ? TREE_OPERAND (expr
, 2)
797 : convert (type
, TREE_OPERAND (expr
, 2)));
803 /* When parsing long initializers, we might end up with a lot of casts.
805 if (TREE_CODE (expr
) == INTEGER_CST
)
806 return fold_convert (type
, expr
);
807 return build1 (CONVERT_EXPR
, type
, expr
);
810 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
812 case FIXED_POINT_TYPE
:
813 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
816 return convert (type
,
817 fold_build1 (REALPART_EXPR
,
818 TREE_TYPE (TREE_TYPE (expr
)), expr
));
821 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
823 error ("can%'t convert between vector values of different size");
824 return error_mark_node
;
826 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
829 error ("aggregate value used where an integer was expected");
830 return convert (type
, integer_zero_node
);
834 /* Convert EXPR to the complex type TYPE in the usual ways. */
837 convert_to_complex (tree type
, tree expr
)
839 tree subtype
= TREE_TYPE (type
);
841 switch (TREE_CODE (TREE_TYPE (expr
)))
844 case FIXED_POINT_TYPE
:
848 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
849 convert (subtype
, integer_zero_node
));
853 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
855 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
857 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
858 return fold_build2 (COMPLEX_EXPR
, type
,
859 convert (subtype
, TREE_OPERAND (expr
, 0)),
860 convert (subtype
, TREE_OPERAND (expr
, 1)));
863 expr
= save_expr (expr
);
865 fold_build2 (COMPLEX_EXPR
, type
,
867 fold_build1 (REALPART_EXPR
,
868 TREE_TYPE (TREE_TYPE (expr
)),
871 fold_build1 (IMAGPART_EXPR
,
872 TREE_TYPE (TREE_TYPE (expr
)),
879 error ("pointer value used where a complex was expected");
880 return convert_to_complex (type
, integer_zero_node
);
883 error ("aggregate value used where a complex was expected");
884 return convert_to_complex (type
, integer_zero_node
);
888 /* Convert EXPR to the vector type TYPE in the usual ways. */
891 convert_to_vector (tree type
, tree expr
)
893 switch (TREE_CODE (TREE_TYPE (expr
)))
897 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
899 error ("can%'t convert between vector values of different size");
900 return error_mark_node
;
902 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
905 error ("can%'t convert value to a vector");
906 return error_mark_node
;
910 /* Convert EXPR to some fixed-point type TYPE.
912 EXPR must be fixed-point, float, integer, or enumeral;
913 in other cases error is called. */
916 convert_to_fixed (tree type
, tree expr
)
918 if (integer_zerop (expr
))
920 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
921 return fixed_zero_node
;
923 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
925 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
926 return fixed_one_node
;
929 switch (TREE_CODE (TREE_TYPE (expr
)))
931 case FIXED_POINT_TYPE
:
936 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
939 return convert (type
,
940 fold_build1 (REALPART_EXPR
,
941 TREE_TYPE (TREE_TYPE (expr
)), expr
));
944 error ("aggregate value used where a fixed-point was expected");
945 return error_mark_node
;