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
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
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. */
28 #include "coretypes.h"
34 #include "langhooks.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. */
43 convert_to_pointer (tree type
, tree expr
)
45 if (TREE_TYPE (expr
) == type
)
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
)))
56 return fold_build1 (NOP_EXPR
, type
, expr
);
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),
65 return fold_build1 (CONVERT_EXPR
, type
, expr
);
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. */
76 strip_float_extensions (tree exp
)
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
)
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
;
98 return build_real (type
, real_value_truncate (TYPE_MODE (type
), orig
));
101 if (!CONVERT_EXPR_P (exp
))
104 sub
= TREE_OPERAND (exp
, 0);
105 subt
= TREE_TYPE (sub
);
106 expt
= TREE_TYPE (exp
);
108 if (!FLOAT_TYPE_P (subt
))
111 if (TYPE_PRECISION (subt
) > TYPE_PRECISION (expt
))
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. */
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) */
133 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
134 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
138 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
175 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
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
);
194 tree arg
= fold (convert_to_real (newtype
, arg0
));
195 expr
= build_call_expr (fn
, 1, arg
);
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
);
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. */
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))));
251 /* Convert (outertype)((innertype0)a+(innertype1)b)
252 into ((newtype)a+(newtype)b) where newtype
253 is the widest mode from all of these. */
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
)))
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
)));
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
)));
307 switch (TREE_CODE (TREE_TYPE (expr
)))
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
);
320 return build1 (FLOAT_EXPR
, type
, expr
);
322 case FIXED_POINT_TYPE
:
323 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
326 return convert (type
,
327 fold_build1 (REALPART_EXPR
,
328 TREE_TYPE (TREE_TYPE (expr
)), expr
));
332 error ("pointer value used where a floating point value was expected");
333 return convert_to_real (type
, integer_zero_node
);
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. */
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. */
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
);
382 CASE_FLT_FN (BUILT_IN_CEIL
):
383 /* Only convert in ISO C99 mode. */
384 if (!TARGET_C99_FUNCTIONS
)
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
);
395 CASE_FLT_FN (BUILT_IN_FLOOR
):
396 /* Only convert in ISO C99 mode. */
397 if (!TARGET_C99_FUNCTIONS
)
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
);
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
);
418 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
419 /* Only convert nearbyint* if we can ignore math exceptions. */
420 if (flag_trapping_math
)
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
);
433 CASE_FLT_FN (BUILT_IN_TRUNC
):
434 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
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
))
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),
459 return fold_convert (type
, expr
);
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
;
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
)
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
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
)))))
499 tem
= fold_unary (code
, type
, expr
);
503 tem
= build1 (code
, type
, expr
);
504 TREE_NO_WARNING (tem
) = 1;
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
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
)),
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
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. */
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)
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,
560 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
561 /* In this case, shifting is like multiplication. */
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
571 tree t
= build_int_cst (type
, 0);
573 /* If the original expression had side-effects, we must
575 if (TREE_SIDE_EFFECTS (expr
))
576 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
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
))))
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. */
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,
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
);
663 typex
= signed_type_for (typex
);
664 return convert (type
,
665 fold_build2 (ex_form
, typex
,
666 convert (typex
, arg0
),
667 convert (typex
, arg1
)));
675 /* This is not correct for ABS_EXPR,
676 since we must test the sign before truncation. */
680 /* Don't do unsigned arithmetic where signed was wanted,
682 if (TYPE_UNSIGNED (TREE_TYPE (expr
)))
683 typex
= unsigned_type_for (type
);
685 typex
= signed_type_for (type
);
686 return convert (type
,
687 fold_build1 (ex_form
, typex
,
689 TREE_OPERAND (expr
, 0))));
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
))))
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
));
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)));
714 return build1 (CONVERT_EXPR
, type
, expr
);
717 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
719 case FIXED_POINT_TYPE
:
720 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
723 return convert (type
,
724 fold_build1 (REALPART_EXPR
,
725 TREE_TYPE (TREE_TYPE (expr
)), expr
));
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
);
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. */
744 convert_to_complex (tree type
, tree expr
)
746 tree subtype
= TREE_TYPE (type
);
748 switch (TREE_CODE (TREE_TYPE (expr
)))
751 case FIXED_POINT_TYPE
:
755 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
756 convert (subtype
, integer_zero_node
));
760 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
762 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
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)));
770 expr
= save_expr (expr
);
772 fold_build2 (COMPLEX_EXPR
, type
,
774 fold_build1 (REALPART_EXPR
,
775 TREE_TYPE (TREE_TYPE (expr
)),
778 fold_build1 (IMAGPART_EXPR
,
779 TREE_TYPE (TREE_TYPE (expr
)),
786 error ("pointer value used where a complex was expected");
787 return convert_to_complex (type
, integer_zero_node
);
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. */
798 convert_to_vector (tree type
, tree expr
)
800 switch (TREE_CODE (TREE_TYPE (expr
)))
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
);
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. */
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
:
843 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
846 return convert (type
,
847 fold_build1 (REALPART_EXPR
,
848 TREE_TYPE (TREE_TYPE (expr
)), expr
));
851 error ("aggregate value used where a fixed-point was expected");
852 return error_mark_node
;