2009-01-19 Iain Sandoe <iain.sandoe@sandoe-acoustics.co.uk>
[official-gcc.git] / gcc / convert.c
blob1a462e7626f52501627abca9016c87bec635713a
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 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
86 REAL_VALUE_TYPE orig;
87 tree type = NULL;
89 orig = TREE_REAL_CST (exp);
90 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
91 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
92 type = float_type_node;
93 else if (TYPE_PRECISION (TREE_TYPE (exp))
94 > TYPE_PRECISION (double_type_node)
95 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
96 type = double_type_node;
97 if (type)
98 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
101 if (!CONVERT_EXPR_P (exp))
102 return exp;
104 sub = TREE_OPERAND (exp, 0);
105 subt = TREE_TYPE (sub);
106 expt = TREE_TYPE (exp);
108 if (!FLOAT_TYPE_P (subt))
109 return exp;
111 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
112 return exp;
114 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
115 return exp;
117 return strip_float_extensions (sub);
121 /* Convert EXPR to some floating-point type TYPE.
123 EXPR must be float, fixed-point, integer, or enumeral;
124 in other cases error is called. */
126 tree
127 convert_to_real (tree type, tree expr)
129 enum built_in_function fcode = builtin_mathfn_code (expr);
130 tree itype = TREE_TYPE (expr);
132 /* Disable until we figure out how to decide whether the functions are
133 present in runtime. */
134 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
135 if (optimize
136 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
137 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
139 switch (fcode)
141 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
142 CASE_MATHFN (ACOS)
143 CASE_MATHFN (ACOSH)
144 CASE_MATHFN (ASIN)
145 CASE_MATHFN (ASINH)
146 CASE_MATHFN (ATAN)
147 CASE_MATHFN (ATANH)
148 CASE_MATHFN (CBRT)
149 CASE_MATHFN (COS)
150 CASE_MATHFN (COSH)
151 CASE_MATHFN (ERF)
152 CASE_MATHFN (ERFC)
153 CASE_MATHFN (EXP)
154 CASE_MATHFN (EXP10)
155 CASE_MATHFN (EXP2)
156 CASE_MATHFN (EXPM1)
157 CASE_MATHFN (FABS)
158 CASE_MATHFN (GAMMA)
159 CASE_MATHFN (J0)
160 CASE_MATHFN (J1)
161 CASE_MATHFN (LGAMMA)
162 CASE_MATHFN (LOG)
163 CASE_MATHFN (LOG10)
164 CASE_MATHFN (LOG1P)
165 CASE_MATHFN (LOG2)
166 CASE_MATHFN (LOGB)
167 CASE_MATHFN (POW10)
168 CASE_MATHFN (SIN)
169 CASE_MATHFN (SINH)
170 CASE_MATHFN (SQRT)
171 CASE_MATHFN (TAN)
172 CASE_MATHFN (TANH)
173 CASE_MATHFN (TGAMMA)
174 CASE_MATHFN (Y0)
175 CASE_MATHFN (Y1)
176 #undef CASE_MATHFN
178 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
179 tree newtype = type;
181 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
182 the both as the safe type for operation. */
183 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
184 newtype = TREE_TYPE (arg0);
186 /* Be careful about integer to fp conversions.
187 These may overflow still. */
188 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
189 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
190 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
191 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
193 tree fn = mathfn_built_in (newtype, fcode);
195 if (fn)
197 tree arg = fold (convert_to_real (newtype, arg0));
198 expr = build_call_expr (fn, 1, arg);
199 if (newtype == type)
200 return expr;
204 default:
205 break;
208 if (optimize
209 && (((fcode == BUILT_IN_FLOORL
210 || fcode == BUILT_IN_CEILL
211 || fcode == BUILT_IN_ROUNDL
212 || fcode == BUILT_IN_RINTL
213 || fcode == BUILT_IN_TRUNCL
214 || fcode == BUILT_IN_NEARBYINTL)
215 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
216 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
217 || ((fcode == BUILT_IN_FLOOR
218 || fcode == BUILT_IN_CEIL
219 || fcode == BUILT_IN_ROUND
220 || fcode == BUILT_IN_RINT
221 || fcode == BUILT_IN_TRUNC
222 || fcode == BUILT_IN_NEARBYINT)
223 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
225 tree fn = mathfn_built_in (type, fcode);
227 if (fn)
229 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
231 /* Make sure (type)arg0 is an extension, otherwise we could end up
232 changing (float)floor(double d) into floorf((float)d), which is
233 incorrect because (float)d uses round-to-nearest and can round
234 up to the next integer. */
235 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
236 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
240 /* Propagate the cast into the operation. */
241 if (itype != type && FLOAT_TYPE_P (type))
242 switch (TREE_CODE (expr))
244 /* Convert (float)-x into -(float)x. This is safe for
245 round-to-nearest rounding mode. */
246 case ABS_EXPR:
247 case NEGATE_EXPR:
248 if (!flag_rounding_math
249 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
250 return build1 (TREE_CODE (expr), type,
251 fold (convert_to_real (type,
252 TREE_OPERAND (expr, 0))));
253 break;
254 /* Convert (outertype)((innertype0)a+(innertype1)b)
255 into ((newtype)a+(newtype)b) where newtype
256 is the widest mode from all of these. */
257 case PLUS_EXPR:
258 case MINUS_EXPR:
259 case MULT_EXPR:
260 case RDIV_EXPR:
262 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
263 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
265 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
266 && FLOAT_TYPE_P (TREE_TYPE (arg1))
267 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
269 tree newtype = type;
271 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
272 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
273 || TYPE_MODE (type) == SDmode)
274 newtype = dfloat32_type_node;
275 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
276 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
277 || TYPE_MODE (type) == DDmode)
278 newtype = dfloat64_type_node;
279 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
280 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
281 || TYPE_MODE (type) == TDmode)
282 newtype = dfloat128_type_node;
283 if (newtype == dfloat32_type_node
284 || newtype == dfloat64_type_node
285 || newtype == dfloat128_type_node)
287 expr = build2 (TREE_CODE (expr), newtype,
288 fold (convert_to_real (newtype, arg0)),
289 fold (convert_to_real (newtype, arg1)));
290 if (newtype == type)
291 return expr;
292 break;
295 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
296 newtype = TREE_TYPE (arg0);
297 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
298 newtype = TREE_TYPE (arg1);
299 /* Sometimes this transformation is safe (cannot
300 change results through affecting double rounding
301 cases) and sometimes it is not. If NEWTYPE is
302 wider than TYPE, e.g. (float)((long double)double
303 + (long double)double) converted to
304 (float)(double + double), the transformation is
305 unsafe regardless of the details of the types
306 involved; double rounding can arise if the result
307 of NEWTYPE arithmetic is a NEWTYPE value half way
308 between two representable TYPE values but the
309 exact value is sufficiently different (in the
310 right direction) for this difference to be
311 visible in ITYPE arithmetic. If NEWTYPE is the
312 same as TYPE, however, the transformation may be
313 safe depending on the types involved: it is safe
314 if the ITYPE has strictly more than twice as many
315 mantissa bits as TYPE, can represent infinities
316 and NaNs if the TYPE can, and has sufficient
317 exponent range for the product or ratio of two
318 values representable in the TYPE to be within the
319 range of normal values of ITYPE. */
320 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
321 && (flag_unsafe_math_optimizations
322 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
323 && real_can_shorten_arithmetic (TYPE_MODE (itype),
324 TYPE_MODE (type)))))
326 expr = build2 (TREE_CODE (expr), newtype,
327 fold (convert_to_real (newtype, arg0)),
328 fold (convert_to_real (newtype, arg1)));
329 if (newtype == type)
330 return expr;
334 break;
335 default:
336 break;
339 switch (TREE_CODE (TREE_TYPE (expr)))
341 case REAL_TYPE:
342 /* Ignore the conversion if we don't need to store intermediate
343 results and neither type is a decimal float. */
344 return build1 ((flag_float_store
345 || DECIMAL_FLOAT_TYPE_P (type)
346 || DECIMAL_FLOAT_TYPE_P (itype))
347 ? CONVERT_EXPR : NOP_EXPR, type, expr);
349 case INTEGER_TYPE:
350 case ENUMERAL_TYPE:
351 case BOOLEAN_TYPE:
352 return build1 (FLOAT_EXPR, type, expr);
354 case FIXED_POINT_TYPE:
355 return build1 (FIXED_CONVERT_EXPR, type, expr);
357 case COMPLEX_TYPE:
358 return convert (type,
359 fold_build1 (REALPART_EXPR,
360 TREE_TYPE (TREE_TYPE (expr)), expr));
362 case POINTER_TYPE:
363 case REFERENCE_TYPE:
364 error ("pointer value used where a floating point value was expected");
365 return convert_to_real (type, integer_zero_node);
367 default:
368 error ("aggregate value used where a float was expected");
369 return convert_to_real (type, integer_zero_node);
373 /* Convert EXPR to some integer (or enum) type TYPE.
375 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
376 fixed-point or vector; in other cases error is called.
378 The result of this is always supposed to be a newly created tree node
379 not in use in any existing structure. */
381 tree
382 convert_to_integer (tree type, tree expr)
384 enum tree_code ex_form = TREE_CODE (expr);
385 tree intype = TREE_TYPE (expr);
386 unsigned int inprec = TYPE_PRECISION (intype);
387 unsigned int outprec = TYPE_PRECISION (type);
389 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
390 be. Consider `enum E = { a, b = (enum E) 3 };'. */
391 if (!COMPLETE_TYPE_P (type))
393 error ("conversion to incomplete type");
394 return error_mark_node;
397 /* Convert e.g. (long)round(d) -> lround(d). */
398 /* If we're converting to char, we may encounter differing behavior
399 between converting from double->char vs double->long->char.
400 We're in "undefined" territory but we prefer to be conservative,
401 so only proceed in "unsafe" math mode. */
402 if (optimize
403 && (flag_unsafe_math_optimizations
404 || (long_integer_type_node
405 && outprec >= TYPE_PRECISION (long_integer_type_node))))
407 tree s_expr = strip_float_extensions (expr);
408 tree s_intype = TREE_TYPE (s_expr);
409 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
410 tree fn = 0;
412 switch (fcode)
414 CASE_FLT_FN (BUILT_IN_CEIL):
415 /* Only convert in ISO C99 mode. */
416 if (!TARGET_C99_FUNCTIONS)
417 break;
418 if (outprec < TYPE_PRECISION (long_integer_type_node)
419 || (outprec == TYPE_PRECISION (long_integer_type_node)
420 && !TYPE_UNSIGNED (type)))
421 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
422 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
423 && !TYPE_UNSIGNED (type))
424 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
425 break;
427 CASE_FLT_FN (BUILT_IN_FLOOR):
428 /* Only convert in ISO C99 mode. */
429 if (!TARGET_C99_FUNCTIONS)
430 break;
431 if (outprec < TYPE_PRECISION (long_integer_type_node)
432 || (outprec == TYPE_PRECISION (long_integer_type_node)
433 && !TYPE_UNSIGNED (type)))
434 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
435 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
436 && !TYPE_UNSIGNED (type))
437 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
438 break;
440 CASE_FLT_FN (BUILT_IN_ROUND):
441 if (outprec < TYPE_PRECISION (long_integer_type_node)
442 || (outprec == TYPE_PRECISION (long_integer_type_node)
443 && !TYPE_UNSIGNED (type)))
444 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
445 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
446 && !TYPE_UNSIGNED (type))
447 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
448 break;
450 CASE_FLT_FN (BUILT_IN_NEARBYINT):
451 /* Only convert nearbyint* if we can ignore math exceptions. */
452 if (flag_trapping_math)
453 break;
454 /* ... Fall through ... */
455 CASE_FLT_FN (BUILT_IN_RINT):
456 if (outprec < TYPE_PRECISION (long_integer_type_node)
457 || (outprec == TYPE_PRECISION (long_integer_type_node)
458 && !TYPE_UNSIGNED (type)))
459 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
460 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
461 && !TYPE_UNSIGNED (type))
462 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
463 break;
465 CASE_FLT_FN (BUILT_IN_TRUNC):
466 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
468 default:
469 break;
472 if (fn)
474 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
475 return convert_to_integer (type, newexpr);
479 switch (TREE_CODE (intype))
481 case POINTER_TYPE:
482 case REFERENCE_TYPE:
483 if (integer_zerop (expr))
484 return build_int_cst (type, 0);
486 /* Convert to an unsigned integer of the correct width first,
487 and from there widen/truncate to the required type. */
488 expr = fold_build1 (CONVERT_EXPR,
489 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
490 expr);
491 return fold_convert (type, expr);
493 case INTEGER_TYPE:
494 case ENUMERAL_TYPE:
495 case BOOLEAN_TYPE:
496 /* If this is a logical operation, which just returns 0 or 1, we can
497 change the type of the expression. */
499 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
501 expr = copy_node (expr);
502 TREE_TYPE (expr) = type;
503 return expr;
506 /* If we are widening the type, put in an explicit conversion.
507 Similarly if we are not changing the width. After this, we know
508 we are truncating EXPR. */
510 else if (outprec >= inprec)
512 enum tree_code code;
513 tree tem;
515 /* If the precision of the EXPR's type is K bits and the
516 destination mode has more bits, and the sign is changing,
517 it is not safe to use a NOP_EXPR. For example, suppose
518 that EXPR's type is a 3-bit unsigned integer type, the
519 TYPE is a 3-bit signed integer type, and the machine mode
520 for the types is 8-bit QImode. In that case, the
521 conversion necessitates an explicit sign-extension. In
522 the signed-to-unsigned case the high-order bits have to
523 be cleared. */
524 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
525 && (TYPE_PRECISION (TREE_TYPE (expr))
526 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
527 code = CONVERT_EXPR;
528 else
529 code = NOP_EXPR;
531 tem = fold_unary (code, type, expr);
532 if (tem)
533 return tem;
535 tem = build1 (code, type, expr);
536 TREE_NO_WARNING (tem) = 1;
537 return tem;
540 /* If TYPE is an enumeral type or a type with a precision less
541 than the number of bits in its mode, do the conversion to the
542 type corresponding to its mode, then do a nop conversion
543 to TYPE. */
544 else if (TREE_CODE (type) == ENUMERAL_TYPE
545 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
546 return build1 (NOP_EXPR, type,
547 convert (lang_hooks.types.type_for_mode
548 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
549 expr));
551 /* Here detect when we can distribute the truncation down past some
552 arithmetic. For example, if adding two longs and converting to an
553 int, we can equally well convert both to ints and then add.
554 For the operations handled here, such truncation distribution
555 is always safe.
556 It is desirable in these cases:
557 1) when truncating down to full-word from a larger size
558 2) when truncating takes no work.
559 3) when at least one operand of the arithmetic has been extended
560 (as by C's default conversions). In this case we need two conversions
561 if we do the arithmetic as already requested, so we might as well
562 truncate both and then combine. Perhaps that way we need only one.
564 Note that in general we cannot do the arithmetic in a type
565 shorter than the desired result of conversion, even if the operands
566 are both extended from a shorter type, because they might overflow
567 if combined in that type. The exceptions to this--the times when
568 two narrow values can be combined in their narrow type even to
569 make a wider result--are handled by "shorten" in build_binary_op. */
571 switch (ex_form)
573 case RSHIFT_EXPR:
574 /* We can pass truncation down through right shifting
575 when the shift count is a nonpositive constant. */
576 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
577 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
578 goto trunc1;
579 break;
581 case LSHIFT_EXPR:
582 /* We can pass truncation down through left shifting
583 when the shift count is a nonnegative constant and
584 the target type is unsigned. */
585 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
586 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
587 && TYPE_UNSIGNED (type)
588 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
590 /* If shift count is less than the width of the truncated type,
591 really shift. */
592 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
593 /* In this case, shifting is like multiplication. */
594 goto trunc1;
595 else
597 /* If it is >= that width, result is zero.
598 Handling this with trunc1 would give the wrong result:
599 (int) ((long long) a << 32) is well defined (as 0)
600 but (int) a << 32 is undefined and would get a
601 warning. */
603 tree t = build_int_cst (type, 0);
605 /* If the original expression had side-effects, we must
606 preserve it. */
607 if (TREE_SIDE_EFFECTS (expr))
608 return build2 (COMPOUND_EXPR, type, expr, t);
609 else
610 return t;
613 break;
615 case MAX_EXPR:
616 case MIN_EXPR:
617 case MULT_EXPR:
619 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
620 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
622 /* Don't distribute unless the output precision is at least as big
623 as the actual inputs. Otherwise, the comparison of the
624 truncated values will be wrong. */
625 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
626 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
627 /* If signedness of arg0 and arg1 don't match,
628 we can't necessarily find a type to compare them in. */
629 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
630 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
631 goto trunc1;
632 break;
635 case PLUS_EXPR:
636 case MINUS_EXPR:
637 case BIT_AND_EXPR:
638 case BIT_IOR_EXPR:
639 case BIT_XOR_EXPR:
640 trunc1:
642 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
643 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
645 if (outprec >= BITS_PER_WORD
646 || TRULY_NOOP_TRUNCATION (outprec, inprec)
647 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
648 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
650 /* Do the arithmetic in type TYPEX,
651 then convert result to TYPE. */
652 tree typex = type;
654 /* Can't do arithmetic in enumeral types
655 so use an integer type that will hold the values. */
656 if (TREE_CODE (typex) == ENUMERAL_TYPE)
657 typex = lang_hooks.types.type_for_size
658 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
660 /* But now perhaps TYPEX is as wide as INPREC.
661 In that case, do nothing special here.
662 (Otherwise would recurse infinitely in convert. */
663 if (TYPE_PRECISION (typex) != inprec)
665 /* Don't do unsigned arithmetic where signed was wanted,
666 or vice versa.
667 Exception: if both of the original operands were
668 unsigned then we can safely do the work as unsigned.
669 Exception: shift operations take their type solely
670 from the first argument.
671 Exception: the LSHIFT_EXPR case above requires that
672 we perform this operation unsigned lest we produce
673 signed-overflow undefinedness.
674 And we may need to do it as unsigned
675 if we truncate to the original size. */
676 if (TYPE_UNSIGNED (TREE_TYPE (expr))
677 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
678 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
679 || ex_form == LSHIFT_EXPR
680 || ex_form == RSHIFT_EXPR
681 || ex_form == LROTATE_EXPR
682 || ex_form == RROTATE_EXPR))
683 || ex_form == LSHIFT_EXPR
684 /* If we have !flag_wrapv, and either ARG0 or
685 ARG1 is of a signed type, we have to do
686 PLUS_EXPR or MINUS_EXPR in an unsigned
687 type. Otherwise, we would introduce
688 signed-overflow undefinedness. */
689 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
690 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
691 && (ex_form == PLUS_EXPR
692 || ex_form == MINUS_EXPR)))
693 typex = unsigned_type_for (typex);
694 else
695 typex = signed_type_for (typex);
696 return convert (type,
697 fold_build2 (ex_form, typex,
698 convert (typex, arg0),
699 convert (typex, arg1)));
703 break;
705 case NEGATE_EXPR:
706 case BIT_NOT_EXPR:
707 /* This is not correct for ABS_EXPR,
708 since we must test the sign before truncation. */
710 tree typex;
712 /* Don't do unsigned arithmetic where signed was wanted,
713 or vice versa. */
714 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
715 typex = unsigned_type_for (type);
716 else
717 typex = signed_type_for (type);
718 return convert (type,
719 fold_build1 (ex_form, typex,
720 convert (typex,
721 TREE_OPERAND (expr, 0))));
724 case NOP_EXPR:
725 /* Don't introduce a
726 "can't convert between vector values of different size" error. */
727 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
728 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
729 != GET_MODE_SIZE (TYPE_MODE (type))))
730 break;
731 /* If truncating after truncating, might as well do all at once.
732 If truncating after extending, we may get rid of wasted work. */
733 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
735 case COND_EXPR:
736 /* It is sometimes worthwhile to push the narrowing down through
737 the conditional and never loses. */
738 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
739 convert (type, TREE_OPERAND (expr, 1)),
740 convert (type, TREE_OPERAND (expr, 2)));
742 default:
743 break;
746 return build1 (CONVERT_EXPR, type, expr);
748 case REAL_TYPE:
749 return build1 (FIX_TRUNC_EXPR, type, expr);
751 case FIXED_POINT_TYPE:
752 return build1 (FIXED_CONVERT_EXPR, type, expr);
754 case COMPLEX_TYPE:
755 return convert (type,
756 fold_build1 (REALPART_EXPR,
757 TREE_TYPE (TREE_TYPE (expr)), expr));
759 case VECTOR_TYPE:
760 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
762 error ("can't convert between vector values of different size");
763 return error_mark_node;
765 return build1 (VIEW_CONVERT_EXPR, type, expr);
767 default:
768 error ("aggregate value used where an integer was expected");
769 return convert (type, integer_zero_node);
773 /* Convert EXPR to the complex type TYPE in the usual ways. */
775 tree
776 convert_to_complex (tree type, tree expr)
778 tree subtype = TREE_TYPE (type);
780 switch (TREE_CODE (TREE_TYPE (expr)))
782 case REAL_TYPE:
783 case FIXED_POINT_TYPE:
784 case INTEGER_TYPE:
785 case ENUMERAL_TYPE:
786 case BOOLEAN_TYPE:
787 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
788 convert (subtype, integer_zero_node));
790 case COMPLEX_TYPE:
792 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
794 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
795 return expr;
796 else if (TREE_CODE (expr) == COMPLEX_EXPR)
797 return fold_build2 (COMPLEX_EXPR, type,
798 convert (subtype, TREE_OPERAND (expr, 0)),
799 convert (subtype, TREE_OPERAND (expr, 1)));
800 else
802 expr = save_expr (expr);
803 return
804 fold_build2 (COMPLEX_EXPR, type,
805 convert (subtype,
806 fold_build1 (REALPART_EXPR,
807 TREE_TYPE (TREE_TYPE (expr)),
808 expr)),
809 convert (subtype,
810 fold_build1 (IMAGPART_EXPR,
811 TREE_TYPE (TREE_TYPE (expr)),
812 expr)));
816 case POINTER_TYPE:
817 case REFERENCE_TYPE:
818 error ("pointer value used where a complex was expected");
819 return convert_to_complex (type, integer_zero_node);
821 default:
822 error ("aggregate value used where a complex was expected");
823 return convert_to_complex (type, integer_zero_node);
827 /* Convert EXPR to the vector type TYPE in the usual ways. */
829 tree
830 convert_to_vector (tree type, tree expr)
832 switch (TREE_CODE (TREE_TYPE (expr)))
834 case INTEGER_TYPE:
835 case VECTOR_TYPE:
836 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
838 error ("can't convert between vector values of different size");
839 return error_mark_node;
841 return build1 (VIEW_CONVERT_EXPR, type, expr);
843 default:
844 error ("can't convert value to a vector");
845 return error_mark_node;
849 /* Convert EXPR to some fixed-point type TYPE.
851 EXPR must be fixed-point, float, integer, or enumeral;
852 in other cases error is called. */
854 tree
855 convert_to_fixed (tree type, tree expr)
857 if (integer_zerop (expr))
859 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
860 return fixed_zero_node;
862 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
864 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
865 return fixed_one_node;
868 switch (TREE_CODE (TREE_TYPE (expr)))
870 case FIXED_POINT_TYPE:
871 case INTEGER_TYPE:
872 case ENUMERAL_TYPE:
873 case BOOLEAN_TYPE:
874 case REAL_TYPE:
875 return build1 (FIXED_CONVERT_EXPR, type, expr);
877 case COMPLEX_TYPE:
878 return convert (type,
879 fold_build1 (REALPART_EXPR,
880 TREE_TYPE (TREE_TYPE (expr)), expr));
882 default:
883 error ("aggregate value used where a fixed-point was expected");
884 return error_mark_node;