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