OSDN Git Service

Add comment saying file is deprecated
[pf3gnuchains/gcc-fork.git] / gcc / convert.c
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, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22
23 /* These routines are somewhat language-independent utility function
24    intended to be called by the language-specific convert () functions.  */
25
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"
37
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.  */
41
42 tree
43 convert_to_pointer (tree type, tree expr)
44 {
45   location_t loc = EXPR_LOCATION (expr);
46   if (TREE_TYPE (expr) == type)
47     return expr;
48
49   /* Propagate overflow to the NULL pointer.  */
50   if (integer_zerop (expr))
51     return force_fit_type_double (type, 0, 0, 0, TREE_OVERFLOW (expr));
52
53   switch (TREE_CODE (TREE_TYPE (expr)))
54     {
55     case POINTER_TYPE:
56     case REFERENCE_TYPE:
57       return fold_build1_loc (loc, NOP_EXPR, type, expr);
58
59     case INTEGER_TYPE:
60     case ENUMERAL_TYPE:
61     case BOOLEAN_TYPE:
62       {
63         /* If the input precision differs from the target pointer type
64            precision, first convert the input expression to an integer type of
65            the target precision.  Some targets, e.g. VMS, need several pointer
66            sizes to coexist so the latter isn't necessarily POINTER_SIZE.  */
67         unsigned int pprec = TYPE_PRECISION (type);
68         unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
69
70         if (eprec != pprec)
71           expr = fold_build1_loc (loc, NOP_EXPR,
72                               lang_hooks.types.type_for_size (pprec, 0),
73                               expr);
74       }
75
76       return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
77
78     default:
79       error ("cannot convert to a pointer type");
80       return convert_to_pointer (type, integer_zero_node);
81     }
82 }
83
84 /* Avoid any floating point extensions from EXP.  */
85 tree
86 strip_float_extensions (tree exp)
87 {
88   tree sub, expt, subt;
89
90   /*  For floating point constant look up the narrowest type that can hold
91       it properly and handle it like (type)(narrowest_type)constant.
92       This way we can optimize for instance a=a*2.0 where "a" is float
93       but 2.0 is double constant.  */
94   if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
95     {
96       REAL_VALUE_TYPE orig;
97       tree type = NULL;
98
99       orig = TREE_REAL_CST (exp);
100       if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
101           && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
102         type = float_type_node;
103       else if (TYPE_PRECISION (TREE_TYPE (exp))
104                > TYPE_PRECISION (double_type_node)
105                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
106         type = double_type_node;
107       if (type)
108         return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
109     }
110
111   if (!CONVERT_EXPR_P (exp))
112     return exp;
113
114   sub = TREE_OPERAND (exp, 0);
115   subt = TREE_TYPE (sub);
116   expt = TREE_TYPE (exp);
117
118   if (!FLOAT_TYPE_P (subt))
119     return exp;
120
121   if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
122     return exp;
123
124   if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
125     return exp;
126
127   return strip_float_extensions (sub);
128 }
129
130
131 /* Convert EXPR to some floating-point type TYPE.
132
133    EXPR must be float, fixed-point, integer, or enumeral;
134    in other cases error is called.  */
135
136 tree
137 convert_to_real (tree type, tree expr)
138 {
139   enum built_in_function fcode = builtin_mathfn_code (expr);
140   tree itype = TREE_TYPE (expr);
141
142   /* Disable until we figure out how to decide whether the functions are
143      present in runtime.  */
144   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
145   if (optimize
146       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
147           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
148     {
149       switch (fcode)
150         {
151 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
152           CASE_MATHFN (COSH)
153           CASE_MATHFN (EXP)
154           CASE_MATHFN (EXP10)
155           CASE_MATHFN (EXP2)
156           CASE_MATHFN (EXPM1)
157           CASE_MATHFN (GAMMA)
158           CASE_MATHFN (J0)
159           CASE_MATHFN (J1)
160           CASE_MATHFN (LGAMMA)
161           CASE_MATHFN (POW10)
162           CASE_MATHFN (SINH)
163           CASE_MATHFN (TGAMMA)
164           CASE_MATHFN (Y0)
165           CASE_MATHFN (Y1)
166             /* The above functions may set errno differently with float
167                input or output so this transformation is not safe with
168                -fmath-errno.  */
169             if (flag_errno_math)
170               break;
171           CASE_MATHFN (ACOS)
172           CASE_MATHFN (ACOSH)
173           CASE_MATHFN (ASIN)
174           CASE_MATHFN (ASINH)
175           CASE_MATHFN (ATAN)
176           CASE_MATHFN (ATANH)
177           CASE_MATHFN (CBRT)
178           CASE_MATHFN (COS)
179           CASE_MATHFN (ERF)
180           CASE_MATHFN (ERFC)
181           CASE_MATHFN (FABS)
182           CASE_MATHFN (LOG)
183           CASE_MATHFN (LOG10)
184           CASE_MATHFN (LOG2)
185           CASE_MATHFN (LOG1P)
186           CASE_MATHFN (LOGB)
187           CASE_MATHFN (SIN)
188           CASE_MATHFN (SQRT)
189           CASE_MATHFN (TAN)
190           CASE_MATHFN (TANH)
191 #undef CASE_MATHFN
192             {
193               tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
194               tree newtype = type;
195
196               /* We have (outertype)sqrt((innertype)x).  Choose the wider mode from
197                  the both as the safe type for operation.  */
198               if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
199                 newtype = TREE_TYPE (arg0);
200
201               /* Be careful about integer to fp conversions.
202                  These may overflow still.  */
203               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
204                   && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
205                   && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
206                       || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
207                 {
208                   tree fn = mathfn_built_in (newtype, fcode);
209
210                   if (fn)
211                   {
212                     tree arg = fold (convert_to_real (newtype, arg0));
213                     expr = build_call_expr (fn, 1, arg);
214                     if (newtype == type)
215                       return expr;
216                   }
217                 }
218             }
219         default:
220           break;
221         }
222     }
223   if (optimize
224       && (((fcode == BUILT_IN_FLOORL
225            || fcode == BUILT_IN_CEILL
226            || fcode == BUILT_IN_ROUNDL
227            || fcode == BUILT_IN_RINTL
228            || fcode == BUILT_IN_TRUNCL
229            || fcode == BUILT_IN_NEARBYINTL)
230           && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
231               || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
232           || ((fcode == BUILT_IN_FLOOR
233                || fcode == BUILT_IN_CEIL
234                || fcode == BUILT_IN_ROUND
235                || fcode == BUILT_IN_RINT
236                || fcode == BUILT_IN_TRUNC
237                || fcode == BUILT_IN_NEARBYINT)
238               && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
239     {
240       tree fn = mathfn_built_in (type, fcode);
241
242       if (fn)
243         {
244           tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
245
246           /* Make sure (type)arg0 is an extension, otherwise we could end up
247              changing (float)floor(double d) into floorf((float)d), which is
248              incorrect because (float)d uses round-to-nearest and can round
249              up to the next integer.  */
250           if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
251             return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
252         }
253     }
254
255   /* Propagate the cast into the operation.  */
256   if (itype != type && FLOAT_TYPE_P (type))
257     switch (TREE_CODE (expr))
258       {
259         /* Convert (float)-x into -(float)x.  This is safe for
260            round-to-nearest rounding mode.  */
261         case ABS_EXPR:
262         case NEGATE_EXPR:
263           if (!flag_rounding_math
264               && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
265             return build1 (TREE_CODE (expr), type,
266                            fold (convert_to_real (type,
267                                                   TREE_OPERAND (expr, 0))));
268           break;
269         /* Convert (outertype)((innertype0)a+(innertype1)b)
270            into ((newtype)a+(newtype)b) where newtype
271            is the widest mode from all of these.  */
272         case PLUS_EXPR:
273         case MINUS_EXPR:
274         case MULT_EXPR:
275         case RDIV_EXPR:
276            {
277              tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
278              tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
279
280              if (FLOAT_TYPE_P (TREE_TYPE (arg0))
281                  && FLOAT_TYPE_P (TREE_TYPE (arg1))
282                  && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
283                {
284                   tree newtype = type;
285
286                   if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
287                       || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
288                       || TYPE_MODE (type) == SDmode)
289                     newtype = dfloat32_type_node;
290                   if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
291                       || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
292                       || TYPE_MODE (type) == DDmode)
293                     newtype = dfloat64_type_node;
294                   if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
295                       || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
296                       || TYPE_MODE (type) == TDmode)
297                     newtype = dfloat128_type_node;
298                   if (newtype == dfloat32_type_node
299                       || newtype == dfloat64_type_node
300                       || newtype == dfloat128_type_node)
301                     {
302                       expr = build2 (TREE_CODE (expr), newtype,
303                                      fold (convert_to_real (newtype, arg0)),
304                                      fold (convert_to_real (newtype, arg1)));
305                       if (newtype == type)
306                         return expr;
307                       break;
308                     }
309
310                   if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
311                     newtype = TREE_TYPE (arg0);
312                   if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
313                     newtype = TREE_TYPE (arg1);
314                   /* Sometimes this transformation is safe (cannot
315                      change results through affecting double rounding
316                      cases) and sometimes it is not.  If NEWTYPE is
317                      wider than TYPE, e.g. (float)((long double)double
318                      + (long double)double) converted to
319                      (float)(double + double), the transformation is
320                      unsafe regardless of the details of the types
321                      involved; double rounding can arise if the result
322                      of NEWTYPE arithmetic is a NEWTYPE value half way
323                      between two representable TYPE values but the
324                      exact value is sufficiently different (in the
325                      right direction) for this difference to be
326                      visible in ITYPE arithmetic.  If NEWTYPE is the
327                      same as TYPE, however, the transformation may be
328                      safe depending on the types involved: it is safe
329                      if the ITYPE has strictly more than twice as many
330                      mantissa bits as TYPE, can represent infinities
331                      and NaNs if the TYPE can, and has sufficient
332                      exponent range for the product or ratio of two
333                      values representable in the TYPE to be within the
334                      range of normal values of ITYPE.  */
335                   if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
336                       && (flag_unsafe_math_optimizations
337                           || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
338                               && real_can_shorten_arithmetic (TYPE_MODE (itype),
339                                                               TYPE_MODE (type))
340                               && !excess_precision_type (newtype))))
341                     {
342                       expr = build2 (TREE_CODE (expr), newtype,
343                                      fold (convert_to_real (newtype, arg0)),
344                                      fold (convert_to_real (newtype, arg1)));
345                       if (newtype == type)
346                         return expr;
347                     }
348                }
349            }
350           break;
351         default:
352           break;
353       }
354
355   switch (TREE_CODE (TREE_TYPE (expr)))
356     {
357     case REAL_TYPE:
358       /* Ignore the conversion if we don't need to store intermediate
359          results and neither type is a decimal float.  */
360       return build1 ((flag_float_store
361                      || DECIMAL_FLOAT_TYPE_P (type)
362                      || DECIMAL_FLOAT_TYPE_P (itype))
363                      ? CONVERT_EXPR : NOP_EXPR, type, expr);
364
365     case INTEGER_TYPE:
366     case ENUMERAL_TYPE:
367     case BOOLEAN_TYPE:
368       return build1 (FLOAT_EXPR, type, expr);
369
370     case FIXED_POINT_TYPE:
371       return build1 (FIXED_CONVERT_EXPR, type, expr);
372
373     case COMPLEX_TYPE:
374       return convert (type,
375                       fold_build1 (REALPART_EXPR,
376                                    TREE_TYPE (TREE_TYPE (expr)), expr));
377
378     case POINTER_TYPE:
379     case REFERENCE_TYPE:
380       error ("pointer value used where a floating point value was expected");
381       return convert_to_real (type, integer_zero_node);
382
383     default:
384       error ("aggregate value used where a float was expected");
385       return convert_to_real (type, integer_zero_node);
386     }
387 }
388
389 /* Convert EXPR to some integer (or enum) type TYPE.
390
391    EXPR must be pointer, integer, discrete (enum, char, or bool), float,
392    fixed-point or vector; in other cases error is called.
393
394    The result of this is always supposed to be a newly created tree node
395    not in use in any existing structure.  */
396
397 tree
398 convert_to_integer (tree type, tree expr)
399 {
400   enum tree_code ex_form = TREE_CODE (expr);
401   tree intype = TREE_TYPE (expr);
402   unsigned int inprec = TYPE_PRECISION (intype);
403   unsigned int outprec = TYPE_PRECISION (type);
404
405   /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
406      be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
407   if (!COMPLETE_TYPE_P (type))
408     {
409       error ("conversion to incomplete type");
410       return error_mark_node;
411     }
412
413   /* Convert e.g. (long)round(d) -> lround(d).  */
414   /* If we're converting to char, we may encounter differing behavior
415      between converting from double->char vs double->long->char.
416      We're in "undefined" territory but we prefer to be conservative,
417      so only proceed in "unsafe" math mode.  */
418   if (optimize
419       && (flag_unsafe_math_optimizations
420           || (long_integer_type_node
421               && outprec >= TYPE_PRECISION (long_integer_type_node))))
422     {
423       tree s_expr = strip_float_extensions (expr);
424       tree s_intype = TREE_TYPE (s_expr);
425       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
426       tree fn = 0;
427       
428       switch (fcode)
429         {
430         CASE_FLT_FN (BUILT_IN_CEIL):
431           /* Only convert in ISO C99 mode.  */
432           if (!TARGET_C99_FUNCTIONS)
433             break;
434           if (outprec < TYPE_PRECISION (long_integer_type_node)
435               || (outprec == TYPE_PRECISION (long_integer_type_node)
436                   && !TYPE_UNSIGNED (type)))
437             fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
438           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
439                    && !TYPE_UNSIGNED (type))
440             fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
441           break;
442
443         CASE_FLT_FN (BUILT_IN_FLOOR):
444           /* Only convert in ISO C99 mode.  */
445           if (!TARGET_C99_FUNCTIONS)
446             break;
447           if (outprec < TYPE_PRECISION (long_integer_type_node)
448               || (outprec == TYPE_PRECISION (long_integer_type_node)
449                   && !TYPE_UNSIGNED (type)))
450             fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
451           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
452                    && !TYPE_UNSIGNED (type))
453             fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
454           break;
455
456         CASE_FLT_FN (BUILT_IN_ROUND):
457           if (outprec < TYPE_PRECISION (long_integer_type_node)
458               || (outprec == TYPE_PRECISION (long_integer_type_node)
459                   && !TYPE_UNSIGNED (type)))
460             fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
461           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
462                    && !TYPE_UNSIGNED (type))
463             fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
464           break;
465
466         CASE_FLT_FN (BUILT_IN_NEARBYINT):
467           /* Only convert nearbyint* if we can ignore math exceptions.  */
468           if (flag_trapping_math)
469             break;
470           /* ... Fall through ...  */
471         CASE_FLT_FN (BUILT_IN_RINT):
472           if (outprec < TYPE_PRECISION (long_integer_type_node)
473               || (outprec == TYPE_PRECISION (long_integer_type_node)
474                   && !TYPE_UNSIGNED (type)))
475             fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
476           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
477                    && !TYPE_UNSIGNED (type))
478             fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
479           break;
480
481         CASE_FLT_FN (BUILT_IN_TRUNC):
482           return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
483
484         default:
485           break;
486         }
487       
488       if (fn)
489         {
490           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
491           return convert_to_integer (type, newexpr);
492         }
493     }
494
495   /* Convert (int)logb(d) -> ilogb(d).  */
496   if (optimize
497       && flag_unsafe_math_optimizations
498       && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
499       && integer_type_node
500       && (outprec > TYPE_PRECISION (integer_type_node)
501           || (outprec == TYPE_PRECISION (integer_type_node)
502               && !TYPE_UNSIGNED (type))))
503     {
504       tree s_expr = strip_float_extensions (expr);
505       tree s_intype = TREE_TYPE (s_expr);
506       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
507       tree fn = 0;
508        
509       switch (fcode)
510         {
511         CASE_FLT_FN (BUILT_IN_LOGB):
512           fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
513           break;
514
515         default:
516           break;
517         }
518
519       if (fn)
520         {
521           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
522           return convert_to_integer (type, newexpr);
523         }
524     }
525
526   switch (TREE_CODE (intype))
527     {
528     case POINTER_TYPE:
529     case REFERENCE_TYPE:
530       if (integer_zerop (expr))
531         return build_int_cst (type, 0);
532
533       /* Convert to an unsigned integer of the correct width first,
534          and from there widen/truncate to the required type.  */
535       expr = fold_build1 (CONVERT_EXPR,
536                           lang_hooks.types.type_for_size (POINTER_SIZE, 0),
537                           expr);
538       return fold_convert (type, expr);
539
540     case INTEGER_TYPE:
541     case ENUMERAL_TYPE:
542     case BOOLEAN_TYPE:
543     case OFFSET_TYPE:
544       /* If this is a logical operation, which just returns 0 or 1, we can
545          change the type of the expression.  */
546
547       if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
548         {
549           expr = copy_node (expr);
550           TREE_TYPE (expr) = type;
551           return expr;
552         }
553
554       /* If we are widening the type, put in an explicit conversion.
555          Similarly if we are not changing the width.  After this, we know
556          we are truncating EXPR.  */
557
558       else if (outprec >= inprec)
559         {
560           enum tree_code code;
561           tree tem;
562
563           /* If the precision of the EXPR's type is K bits and the
564              destination mode has more bits, and the sign is changing,
565              it is not safe to use a NOP_EXPR.  For example, suppose
566              that EXPR's type is a 3-bit unsigned integer type, the
567              TYPE is a 3-bit signed integer type, and the machine mode
568              for the types is 8-bit QImode.  In that case, the
569              conversion necessitates an explicit sign-extension.  In
570              the signed-to-unsigned case the high-order bits have to
571              be cleared.  */
572           if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
573               && (TYPE_PRECISION (TREE_TYPE (expr))
574                   != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
575             code = CONVERT_EXPR;
576           else
577             code = NOP_EXPR;
578
579           tem = fold_unary (code, type, expr);
580           if (tem)
581             return tem;
582
583           tem = build1 (code, type, expr);
584           TREE_NO_WARNING (tem) = 1;
585           return tem;
586         }
587
588       /* If TYPE is an enumeral type or a type with a precision less
589          than the number of bits in its mode, do the conversion to the
590          type corresponding to its mode, then do a nop conversion
591          to TYPE.  */
592       else if (TREE_CODE (type) == ENUMERAL_TYPE
593                || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
594         return build1 (NOP_EXPR, type,
595                        convert (lang_hooks.types.type_for_mode
596                                 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
597                                 expr));
598
599       /* Here detect when we can distribute the truncation down past some
600          arithmetic.  For example, if adding two longs and converting to an
601          int, we can equally well convert both to ints and then add.
602          For the operations handled here, such truncation distribution
603          is always safe.
604          It is desirable in these cases:
605          1) when truncating down to full-word from a larger size
606          2) when truncating takes no work.
607          3) when at least one operand of the arithmetic has been extended
608          (as by C's default conversions).  In this case we need two conversions
609          if we do the arithmetic as already requested, so we might as well
610          truncate both and then combine.  Perhaps that way we need only one.
611
612          Note that in general we cannot do the arithmetic in a type
613          shorter than the desired result of conversion, even if the operands
614          are both extended from a shorter type, because they might overflow
615          if combined in that type.  The exceptions to this--the times when
616          two narrow values can be combined in their narrow type even to
617          make a wider result--are handled by "shorten" in build_binary_op.  */
618
619       switch (ex_form)
620         {
621         case RSHIFT_EXPR:
622           /* We can pass truncation down through right shifting
623              when the shift count is a nonpositive constant.  */
624           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
625               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
626             goto trunc1;
627           break;
628
629         case LSHIFT_EXPR:
630           /* We can pass truncation down through left shifting
631              when the shift count is a nonnegative constant and
632              the target type is unsigned.  */
633           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
634               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
635               && TYPE_UNSIGNED (type)
636               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
637             {
638               /* If shift count is less than the width of the truncated type,
639                  really shift.  */
640               if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
641                 /* In this case, shifting is like multiplication.  */
642                 goto trunc1;
643               else
644                 {
645                   /* If it is >= that width, result is zero.
646                      Handling this with trunc1 would give the wrong result:
647                      (int) ((long long) a << 32) is well defined (as 0)
648                      but (int) a << 32 is undefined and would get a
649                      warning.  */
650
651                   tree t = build_int_cst (type, 0);
652
653                   /* If the original expression had side-effects, we must
654                      preserve it.  */
655                   if (TREE_SIDE_EFFECTS (expr))
656                     return build2 (COMPOUND_EXPR, type, expr, t);
657                   else
658                     return t;
659                 }
660             }
661           break;
662
663         case MAX_EXPR:
664         case MIN_EXPR:
665         case MULT_EXPR:
666           {
667             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
668             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
669
670             /* Don't distribute unless the output precision is at least as big
671                as the actual inputs.  Otherwise, the comparison of the
672                truncated values will be wrong.  */
673             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
674                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
675                 /* If signedness of arg0 and arg1 don't match,
676                    we can't necessarily find a type to compare them in.  */
677                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
678                     == TYPE_UNSIGNED (TREE_TYPE (arg1))))
679               goto trunc1;
680             break;
681           }
682
683         case PLUS_EXPR:
684         case MINUS_EXPR:
685         case BIT_AND_EXPR:
686         case BIT_IOR_EXPR:
687         case BIT_XOR_EXPR:
688         trunc1:
689           {
690             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
691             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
692
693             if (outprec >= BITS_PER_WORD
694                 || TRULY_NOOP_TRUNCATION (outprec, inprec)
695                 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
696                 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
697               {
698                 /* Do the arithmetic in type TYPEX,
699                    then convert result to TYPE.  */
700                 tree typex = type;
701
702                 /* Can't do arithmetic in enumeral types
703                    so use an integer type that will hold the values.  */
704                 if (TREE_CODE (typex) == ENUMERAL_TYPE)
705                   typex = lang_hooks.types.type_for_size
706                     (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
707
708                 /* But now perhaps TYPEX is as wide as INPREC.
709                    In that case, do nothing special here.
710                    (Otherwise would recurse infinitely in convert.  */
711                 if (TYPE_PRECISION (typex) != inprec)
712                   {
713                     /* Don't do unsigned arithmetic where signed was wanted,
714                        or vice versa.
715                        Exception: if both of the original operands were
716                        unsigned then we can safely do the work as unsigned.
717                        Exception: shift operations take their type solely
718                        from the first argument.
719                        Exception: the LSHIFT_EXPR case above requires that
720                        we perform this operation unsigned lest we produce
721                        signed-overflow undefinedness.
722                        And we may need to do it as unsigned
723                        if we truncate to the original size.  */
724                     if (TYPE_UNSIGNED (TREE_TYPE (expr))
725                         || (TYPE_UNSIGNED (TREE_TYPE (arg0))
726                             && (TYPE_UNSIGNED (TREE_TYPE (arg1))
727                                 || ex_form == LSHIFT_EXPR
728                                 || ex_form == RSHIFT_EXPR
729                                 || ex_form == LROTATE_EXPR
730                                 || ex_form == RROTATE_EXPR))
731                         || ex_form == LSHIFT_EXPR
732                         /* If we have !flag_wrapv, and either ARG0 or
733                            ARG1 is of a signed type, we have to do
734                            PLUS_EXPR or MINUS_EXPR in an unsigned
735                            type.  Otherwise, we would introduce
736                            signed-overflow undefinedness.  */
737                         || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
738                              || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
739                             && (ex_form == PLUS_EXPR
740                                 || ex_form == MINUS_EXPR)))
741                       typex = unsigned_type_for (typex);
742                     else
743                       typex = signed_type_for (typex);
744                     return convert (type,
745                                     fold_build2 (ex_form, typex,
746                                                  convert (typex, arg0),
747                                                  convert (typex, arg1)));
748                   }
749               }
750           }
751           break;
752
753         case NEGATE_EXPR:
754         case BIT_NOT_EXPR:
755           /* This is not correct for ABS_EXPR,
756              since we must test the sign before truncation.  */
757           {
758             tree typex;
759
760             /* Don't do unsigned arithmetic where signed was wanted,
761                or vice versa.  */
762             if (TYPE_UNSIGNED (TREE_TYPE (expr)))
763               typex = unsigned_type_for (type);
764             else
765               typex = signed_type_for (type);
766             return convert (type,
767                             fold_build1 (ex_form, typex,
768                                          convert (typex,
769                                                   TREE_OPERAND (expr, 0))));
770           }
771
772         case NOP_EXPR:
773           /* Don't introduce a
774              "can't convert between vector values of different size" error.  */
775           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
776               && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
777                   != GET_MODE_SIZE (TYPE_MODE (type))))
778             break;
779           /* If truncating after truncating, might as well do all at once.
780              If truncating after extending, we may get rid of wasted work.  */
781           return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
782
783         case COND_EXPR:
784           /* It is sometimes worthwhile to push the narrowing down through
785              the conditional and never loses.  A COND_EXPR may have a throw
786              as one operand, which then has void type.  Just leave void
787              operands as they are.  */
788           return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
789                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
790                               ? TREE_OPERAND (expr, 1)
791                               : convert (type, TREE_OPERAND (expr, 1)),
792                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
793                               ? TREE_OPERAND (expr, 2)
794                               : convert (type, TREE_OPERAND (expr, 2)));
795
796         default:
797           break;
798         }
799
800       return build1 (CONVERT_EXPR, type, expr);
801
802     case REAL_TYPE:
803       return build1 (FIX_TRUNC_EXPR, type, expr);
804
805     case FIXED_POINT_TYPE:
806       return build1 (FIXED_CONVERT_EXPR, type, expr);
807
808     case COMPLEX_TYPE:
809       return convert (type,
810                       fold_build1 (REALPART_EXPR,
811                                    TREE_TYPE (TREE_TYPE (expr)), expr));
812
813     case VECTOR_TYPE:
814       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
815         {
816           error ("can't convert between vector values of different size");
817           return error_mark_node;
818         }
819       return build1 (VIEW_CONVERT_EXPR, type, expr);
820
821     default:
822       error ("aggregate value used where an integer was expected");
823       return convert (type, integer_zero_node);
824     }
825 }
826
827 /* Convert EXPR to the complex type TYPE in the usual ways.  */
828
829 tree
830 convert_to_complex (tree type, tree expr)
831 {
832   tree subtype = TREE_TYPE (type);
833
834   switch (TREE_CODE (TREE_TYPE (expr)))
835     {
836     case REAL_TYPE:
837     case FIXED_POINT_TYPE:
838     case INTEGER_TYPE:
839     case ENUMERAL_TYPE:
840     case BOOLEAN_TYPE:
841       return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
842                      convert (subtype, integer_zero_node));
843
844     case COMPLEX_TYPE:
845       {
846         tree elt_type = TREE_TYPE (TREE_TYPE (expr));
847
848         if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
849           return expr;
850         else if (TREE_CODE (expr) == COMPLEX_EXPR)
851           return fold_build2 (COMPLEX_EXPR, type,
852                               convert (subtype, TREE_OPERAND (expr, 0)),
853                               convert (subtype, TREE_OPERAND (expr, 1)));
854         else
855           {
856             expr = save_expr (expr);
857             return
858               fold_build2 (COMPLEX_EXPR, type,
859                            convert (subtype,
860                                     fold_build1 (REALPART_EXPR,
861                                                  TREE_TYPE (TREE_TYPE (expr)),
862                                                  expr)),
863                            convert (subtype,
864                                     fold_build1 (IMAGPART_EXPR,
865                                                  TREE_TYPE (TREE_TYPE (expr)),
866                                                  expr)));
867           }
868       }
869
870     case POINTER_TYPE:
871     case REFERENCE_TYPE:
872       error ("pointer value used where a complex was expected");
873       return convert_to_complex (type, integer_zero_node);
874
875     default:
876       error ("aggregate value used where a complex was expected");
877       return convert_to_complex (type, integer_zero_node);
878     }
879 }
880
881 /* Convert EXPR to the vector type TYPE in the usual ways.  */
882
883 tree
884 convert_to_vector (tree type, tree expr)
885 {
886   switch (TREE_CODE (TREE_TYPE (expr)))
887     {
888     case INTEGER_TYPE:
889     case VECTOR_TYPE:
890       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
891         {
892           error ("can't convert between vector values of different size");
893           return error_mark_node;
894         }
895       return build1 (VIEW_CONVERT_EXPR, type, expr);
896
897     default:
898       error ("can't convert value to a vector");
899       return error_mark_node;
900     }
901 }
902
903 /* Convert EXPR to some fixed-point type TYPE.
904
905    EXPR must be fixed-point, float, integer, or enumeral;
906    in other cases error is called.  */
907
908 tree
909 convert_to_fixed (tree type, tree expr)
910 {
911   if (integer_zerop (expr))
912     {
913       tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
914       return fixed_zero_node;
915     }
916   else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
917     {
918       tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
919       return fixed_one_node;
920     }
921
922   switch (TREE_CODE (TREE_TYPE (expr)))
923     {
924     case FIXED_POINT_TYPE:
925     case INTEGER_TYPE:
926     case ENUMERAL_TYPE:
927     case BOOLEAN_TYPE:
928     case REAL_TYPE:
929       return build1 (FIXED_CONVERT_EXPR, type, expr);
930
931     case COMPLEX_TYPE:
932       return convert (type,
933                       fold_build1 (REALPART_EXPR,
934                                    TREE_TYPE (TREE_TYPE (expr)), expr));
935
936     default:
937       error ("aggregate value used where a fixed-point was expected");
938       return error_mark_node;
939     }
940 }