PR libstdc++/9527, PR libstdc++/8713
[official-gcc.git] / gcc / convert.c
blob73db933b97e27b201f23e5b4cba60480a6c2a83a
1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997,
3 1998 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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 /* 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. */
41 tree
42 convert_to_pointer (type, expr)
43 tree type, expr;
45 if (integer_zerop (expr))
47 expr = build_int_2 (0, 0);
48 TREE_TYPE (expr) = type;
49 return expr;
52 switch (TREE_CODE (TREE_TYPE (expr)))
54 case POINTER_TYPE:
55 case REFERENCE_TYPE:
56 return build1 (NOP_EXPR, type, expr);
58 case INTEGER_TYPE:
59 case ENUMERAL_TYPE:
60 case BOOLEAN_TYPE:
61 case CHAR_TYPE:
62 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
63 return build1 (CONVERT_EXPR, type, expr);
65 return
66 convert_to_pointer (type,
67 convert ((*lang_hooks.types.type_for_size)
68 (POINTER_SIZE, 0), expr));
70 default:
71 error ("cannot convert to a pointer type");
72 return convert_to_pointer (type, integer_zero_node);
76 /* Avoid any floating point extensions from EXP. */
77 tree
78 strip_float_extensions (exp)
79 tree exp;
81 tree sub, expt, subt;
83 /* For floating point constant look up the narrowest type that can hold
84 it properly and handle it like (type)(narrowest_type)constant.
85 This way we can optimize for instance a=a*2.0 where "a" is float
86 but 2.0 is double constant. */
87 if (TREE_CODE (exp) == REAL_CST)
89 REAL_VALUE_TYPE orig;
90 tree type = NULL;
92 orig = TREE_REAL_CST (exp);
93 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
94 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
95 type = float_type_node;
96 else if (TYPE_PRECISION (TREE_TYPE (exp))
97 > TYPE_PRECISION (double_type_node)
98 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
99 type = double_type_node;
100 if (type)
101 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
104 if (TREE_CODE (exp) != NOP_EXPR)
105 return exp;
107 sub = TREE_OPERAND (exp, 0);
108 subt = TREE_TYPE (sub);
109 expt = TREE_TYPE (exp);
111 if (!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, integer, or enumeral;
124 in other cases error is called. */
126 tree
127 convert_to_real (type, expr)
128 tree type, expr;
130 enum built_in_function fcode = builtin_mathfn_code (expr);
131 tree itype = TREE_TYPE (expr);
133 /* Disable until we figure out how to decide whether the functions are
134 present in runtime. */
135 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
136 if ((fcode == BUILT_IN_SQRT
137 || fcode == BUILT_IN_SQRTL
138 || fcode == BUILT_IN_SIN
139 || fcode == BUILT_IN_SINL
140 || fcode == BUILT_IN_COS
141 || fcode == BUILT_IN_COSL
142 || fcode == BUILT_IN_EXP
143 || fcode == BUILT_IN_EXPL)
144 && optimize
145 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
146 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
148 tree arg0 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
149 tree newtype = type;
151 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
152 the both as the safe type for operation. */
153 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
154 newtype = TREE_TYPE (arg0);
156 /* Be curefull about integer to fp conversions.
157 These may overflow still. */
158 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
159 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
160 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
161 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
163 tree arglist;
164 tree fn = mathfn_built_in (newtype, fcode);
166 if (fn)
168 arglist = build_tree_list (NULL_TREE, fold (convert_to_real (newtype, arg0)));
169 expr = build_function_call_expr (fn, arglist);
170 if (newtype == type)
171 return expr;
175 if (optimize
176 && (((fcode == BUILT_IN_FLOORL
177 || fcode == BUILT_IN_CEILL
178 || fcode == BUILT_IN_ROUND
179 || fcode == BUILT_IN_TRUNC
180 || fcode == BUILT_IN_NEARBYINT)
181 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
182 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
183 || ((fcode == BUILT_IN_FLOOR
184 || fcode == BUILT_IN_CEIL
185 || fcode == BUILT_IN_ROUND
186 || fcode == BUILT_IN_TRUNC
187 || fcode == BUILT_IN_NEARBYINT)
188 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
190 tree fn = mathfn_built_in (type, fcode);
192 if (fn)
194 tree arg0 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr,
195 1)));
196 tree arglist = build_tree_list (NULL_TREE,
197 fold (convert_to_real (type, arg0)));
199 return build_function_call_expr (fn, arglist);
203 /* Propagate the cast into the operation. */
204 if (itype != type && FLOAT_TYPE_P (type))
205 switch (TREE_CODE (expr))
207 /* convert (float)-x into -(float)x. This is always safe. */
208 case ABS_EXPR:
209 case NEGATE_EXPR:
210 if (TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
211 return build1 (TREE_CODE (expr), type,
212 fold (convert_to_real (type,
213 TREE_OPERAND (expr, 0))));
214 break;
215 /* convert (outertype)((innertype0)a+(innertype1)b)
216 into ((newtype)a+(newtype)b) where newtype
217 is the widest mode from all of these. */
218 case PLUS_EXPR:
219 case MINUS_EXPR:
220 case MULT_EXPR:
221 case RDIV_EXPR:
223 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
224 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
226 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
227 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
229 tree newtype = type;
230 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
231 newtype = TREE_TYPE (arg0);
232 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
233 newtype = TREE_TYPE (arg1);
234 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
236 expr = build (TREE_CODE (expr), newtype,
237 fold (convert_to_real (newtype, arg0)),
238 fold (convert_to_real (newtype, arg1)));
239 if (newtype == type)
240 return expr;
244 break;
245 default:
246 break;
249 switch (TREE_CODE (TREE_TYPE (expr)))
251 case REAL_TYPE:
252 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
253 type, expr);
255 case INTEGER_TYPE:
256 case ENUMERAL_TYPE:
257 case BOOLEAN_TYPE:
258 case CHAR_TYPE:
259 return build1 (FLOAT_EXPR, type, expr);
261 case COMPLEX_TYPE:
262 return convert (type,
263 fold (build1 (REALPART_EXPR,
264 TREE_TYPE (TREE_TYPE (expr)), expr)));
266 case POINTER_TYPE:
267 case REFERENCE_TYPE:
268 error ("pointer value used where a floating point value was expected");
269 return convert_to_real (type, integer_zero_node);
271 default:
272 error ("aggregate value used where a float was expected");
273 return convert_to_real (type, integer_zero_node);
277 /* Convert EXPR to some integer (or enum) type TYPE.
279 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
280 vector; in other cases error is called.
282 The result of this is always supposed to be a newly created tree node
283 not in use in any existing structure. */
285 tree
286 convert_to_integer (type, expr)
287 tree type, expr;
289 enum tree_code ex_form = TREE_CODE (expr);
290 tree intype = TREE_TYPE (expr);
291 unsigned int inprec = TYPE_PRECISION (intype);
292 unsigned int outprec = TYPE_PRECISION (type);
294 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
295 be. Consider `enum E = { a, b = (enum E) 3 };'. */
296 if (!COMPLETE_TYPE_P (type))
298 error ("conversion to incomplete type");
299 return error_mark_node;
302 switch (TREE_CODE (intype))
304 case POINTER_TYPE:
305 case REFERENCE_TYPE:
306 if (integer_zerop (expr))
307 expr = integer_zero_node;
308 else
309 expr = fold (build1 (CONVERT_EXPR, (*lang_hooks.types.type_for_size)
310 (POINTER_SIZE, 0), expr));
312 return convert_to_integer (type, expr);
314 case INTEGER_TYPE:
315 case ENUMERAL_TYPE:
316 case BOOLEAN_TYPE:
317 case CHAR_TYPE:
318 /* If this is a logical operation, which just returns 0 or 1, we can
319 change the type of the expression. For some logical operations,
320 we must also change the types of the operands to maintain type
321 correctness. */
323 if (TREE_CODE_CLASS (ex_form) == '<')
325 TREE_TYPE (expr) = type;
326 return expr;
329 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
330 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
331 || ex_form == TRUTH_XOR_EXPR)
333 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
334 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
335 TREE_TYPE (expr) = type;
336 return expr;
339 else if (ex_form == TRUTH_NOT_EXPR)
341 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
342 TREE_TYPE (expr) = type;
343 return expr;
346 /* If we are widening the type, put in an explicit conversion.
347 Similarly if we are not changing the width. After this, we know
348 we are truncating EXPR. */
350 else if (outprec >= inprec)
351 return build1 (NOP_EXPR, type, expr);
353 /* If TYPE is an enumeral type or a type with a precision less
354 than the number of bits in its mode, do the conversion to the
355 type corresponding to its mode, then do a nop conversion
356 to TYPE. */
357 else if (TREE_CODE (type) == ENUMERAL_TYPE
358 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
359 return build1 (NOP_EXPR, type,
360 convert ((*lang_hooks.types.type_for_mode)
361 (TYPE_MODE (type), TREE_UNSIGNED (type)),
362 expr));
364 /* Here detect when we can distribute the truncation down past some
365 arithmetic. For example, if adding two longs and converting to an
366 int, we can equally well convert both to ints and then add.
367 For the operations handled here, such truncation distribution
368 is always safe.
369 It is desirable in these cases:
370 1) when truncating down to full-word from a larger size
371 2) when truncating takes no work.
372 3) when at least one operand of the arithmetic has been extended
373 (as by C's default conversions). In this case we need two conversions
374 if we do the arithmetic as already requested, so we might as well
375 truncate both and then combine. Perhaps that way we need only one.
377 Note that in general we cannot do the arithmetic in a type
378 shorter than the desired result of conversion, even if the operands
379 are both extended from a shorter type, because they might overflow
380 if combined in that type. The exceptions to this--the times when
381 two narrow values can be combined in their narrow type even to
382 make a wider result--are handled by "shorten" in build_binary_op. */
384 switch (ex_form)
386 case RSHIFT_EXPR:
387 /* We can pass truncation down through right shifting
388 when the shift count is a nonpositive constant. */
389 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
390 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
391 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
392 integer_one_node)))
393 goto trunc1;
394 break;
396 case LSHIFT_EXPR:
397 /* We can pass truncation down through left shifting
398 when the shift count is a nonnegative constant and
399 the target type is unsigned. */
400 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
401 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
402 && TREE_UNSIGNED (type)
403 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
405 /* If shift count is less than the width of the truncated type,
406 really shift. */
407 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
408 /* In this case, shifting is like multiplication. */
409 goto trunc1;
410 else
412 /* If it is >= that width, result is zero.
413 Handling this with trunc1 would give the wrong result:
414 (int) ((long long) a << 32) is well defined (as 0)
415 but (int) a << 32 is undefined and would get a
416 warning. */
418 tree t = convert_to_integer (type, integer_zero_node);
420 /* If the original expression had side-effects, we must
421 preserve it. */
422 if (TREE_SIDE_EFFECTS (expr))
423 return build (COMPOUND_EXPR, type, expr, t);
424 else
425 return t;
428 break;
430 case MAX_EXPR:
431 case MIN_EXPR:
432 case MULT_EXPR:
434 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
435 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
437 /* Don't distribute unless the output precision is at least as big
438 as the actual inputs. Otherwise, the comparison of the
439 truncated values will be wrong. */
440 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
441 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
442 /* If signedness of arg0 and arg1 don't match,
443 we can't necessarily find a type to compare them in. */
444 && (TREE_UNSIGNED (TREE_TYPE (arg0))
445 == TREE_UNSIGNED (TREE_TYPE (arg1))))
446 goto trunc1;
447 break;
450 case PLUS_EXPR:
451 case MINUS_EXPR:
452 case BIT_AND_EXPR:
453 case BIT_IOR_EXPR:
454 case BIT_XOR_EXPR:
455 case BIT_ANDTC_EXPR:
456 trunc1:
458 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
459 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
461 if (outprec >= BITS_PER_WORD
462 || TRULY_NOOP_TRUNCATION (outprec, inprec)
463 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
464 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
466 /* Do the arithmetic in type TYPEX,
467 then convert result to TYPE. */
468 tree typex = type;
470 /* Can't do arithmetic in enumeral types
471 so use an integer type that will hold the values. */
472 if (TREE_CODE (typex) == ENUMERAL_TYPE)
473 typex = (*lang_hooks.types.type_for_size)
474 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
476 /* But now perhaps TYPEX is as wide as INPREC.
477 In that case, do nothing special here.
478 (Otherwise would recurse infinitely in convert. */
479 if (TYPE_PRECISION (typex) != inprec)
481 /* Don't do unsigned arithmetic where signed was wanted,
482 or vice versa.
483 Exception: if both of the original operands were
484 unsigned then we can safely do the work as unsigned.
485 Exception: shift operations take their type solely
486 from the first argument.
487 Exception: the LSHIFT_EXPR case above requires that
488 we perform this operation unsigned lest we produce
489 signed-overflow undefinedness.
490 And we may need to do it as unsigned
491 if we truncate to the original size. */
492 if (TREE_UNSIGNED (TREE_TYPE (expr))
493 || (TREE_UNSIGNED (TREE_TYPE (arg0))
494 && (TREE_UNSIGNED (TREE_TYPE (arg1))
495 || ex_form == LSHIFT_EXPR
496 || ex_form == RSHIFT_EXPR
497 || ex_form == LROTATE_EXPR
498 || ex_form == RROTATE_EXPR))
499 || ex_form == LSHIFT_EXPR)
500 typex = (*lang_hooks.types.unsigned_type) (typex);
501 else
502 typex = (*lang_hooks.types.signed_type) (typex);
503 return convert (type,
504 fold (build (ex_form, typex,
505 convert (typex, arg0),
506 convert (typex, arg1),
507 0)));
511 break;
513 case NEGATE_EXPR:
514 case BIT_NOT_EXPR:
515 /* This is not correct for ABS_EXPR,
516 since we must test the sign before truncation. */
518 tree typex = type;
520 /* Can't do arithmetic in enumeral types
521 so use an integer type that will hold the values. */
522 if (TREE_CODE (typex) == ENUMERAL_TYPE)
523 typex = (*lang_hooks.types.type_for_size)
524 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
526 /* But now perhaps TYPEX is as wide as INPREC.
527 In that case, do nothing special here.
528 (Otherwise would recurse infinitely in convert. */
529 if (TYPE_PRECISION (typex) != inprec)
531 /* Don't do unsigned arithmetic where signed was wanted,
532 or vice versa. */
533 if (TREE_UNSIGNED (TREE_TYPE (expr)))
534 typex = (*lang_hooks.types.unsigned_type) (typex);
535 else
536 typex = (*lang_hooks.types.signed_type) (typex);
537 return convert (type,
538 fold (build1 (ex_form, typex,
539 convert (typex,
540 TREE_OPERAND (expr, 0)))));
544 case NOP_EXPR:
545 /* Don't introduce a
546 "can't convert between vector values of different size" error. */
547 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
548 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
549 != GET_MODE_SIZE (TYPE_MODE (type))))
550 break;
551 /* If truncating after truncating, might as well do all at once.
552 If truncating after extending, we may get rid of wasted work. */
553 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
555 case COND_EXPR:
556 /* It is sometimes worthwhile to push the narrowing down through
557 the conditional and never loses. */
558 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
559 convert (type, TREE_OPERAND (expr, 1)),
560 convert (type, TREE_OPERAND (expr, 2))));
562 default:
563 break;
566 return build1 (NOP_EXPR, type, expr);
568 case REAL_TYPE:
569 return build1 (FIX_TRUNC_EXPR, type, expr);
571 case COMPLEX_TYPE:
572 return convert (type,
573 fold (build1 (REALPART_EXPR,
574 TREE_TYPE (TREE_TYPE (expr)), expr)));
576 case VECTOR_TYPE:
577 if (GET_MODE_SIZE (TYPE_MODE (type))
578 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
580 error ("can't convert between vector values of different size");
581 return error_mark_node;
583 return build1 (NOP_EXPR, type, expr);
585 default:
586 error ("aggregate value used where an integer was expected");
587 return convert (type, integer_zero_node);
591 /* Convert EXPR to the complex type TYPE in the usual ways. */
593 tree
594 convert_to_complex (type, expr)
595 tree type, expr;
597 tree subtype = TREE_TYPE (type);
599 switch (TREE_CODE (TREE_TYPE (expr)))
601 case REAL_TYPE:
602 case INTEGER_TYPE:
603 case ENUMERAL_TYPE:
604 case BOOLEAN_TYPE:
605 case CHAR_TYPE:
606 return build (COMPLEX_EXPR, type, convert (subtype, expr),
607 convert (subtype, integer_zero_node));
609 case COMPLEX_TYPE:
611 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
613 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
614 return expr;
615 else if (TREE_CODE (expr) == COMPLEX_EXPR)
616 return fold (build (COMPLEX_EXPR,
617 type,
618 convert (subtype, TREE_OPERAND (expr, 0)),
619 convert (subtype, TREE_OPERAND (expr, 1))));
620 else
622 expr = save_expr (expr);
623 return
624 fold (build (COMPLEX_EXPR,
625 type, convert (subtype,
626 fold (build1 (REALPART_EXPR,
627 TREE_TYPE (TREE_TYPE (expr)),
628 expr))),
629 convert (subtype,
630 fold (build1 (IMAGPART_EXPR,
631 TREE_TYPE (TREE_TYPE (expr)),
632 expr)))));
636 case POINTER_TYPE:
637 case REFERENCE_TYPE:
638 error ("pointer value used where a complex was expected");
639 return convert_to_complex (type, integer_zero_node);
641 default:
642 error ("aggregate value used where a complex was expected");
643 return convert_to_complex (type, integer_zero_node);
647 /* Convert EXPR to the vector type TYPE in the usual ways. */
649 tree
650 convert_to_vector (type, expr)
651 tree type, expr;
653 switch (TREE_CODE (TREE_TYPE (expr)))
655 case INTEGER_TYPE:
656 case VECTOR_TYPE:
657 if (GET_MODE_SIZE (TYPE_MODE (type))
658 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
660 error ("can't convert between vector values of different size");
661 return error_mark_node;
663 return build1 (NOP_EXPR, type, expr);
665 default:
666 error ("can't convert value to a vector");
667 return convert_to_vector (type, integer_zero_node);