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
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
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
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
28 #include "coretypes.h"
34 #include "langhooks.h"
35 static tree strip_float_extensions PARAMS ((tree));
37 /* Convert EXPR to some pointer or reference type TYPE.
39 EXPR must be pointer, reference, integer, enumeral, or literal zero;
40 in other cases error is called. */
43 convert_to_pointer (type, expr)
46 if (integer_zerop (expr))
48 expr = build_int_2 (0, 0);
49 TREE_TYPE (expr) = type;
53 switch (TREE_CODE (TREE_TYPE (expr)))
57 return build1 (NOP_EXPR, type, expr);
63 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
64 return build1 (CONVERT_EXPR, type, expr);
67 convert_to_pointer (type,
68 convert ((*lang_hooks.types.type_for_size)
69 (POINTER_SIZE, 0), expr));
72 error ("cannot convert to a pointer type");
73 return convert_to_pointer (type, integer_zero_node);
77 /* Avoid any floating point extensions from EXP. */
79 strip_float_extensions (exp)
84 if (TREE_CODE (exp) != NOP_EXPR)
87 sub = TREE_OPERAND (exp, 0);
88 subt = TREE_TYPE (sub);
89 expt = TREE_TYPE (exp);
91 if (!FLOAT_TYPE_P (subt))
94 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
97 return strip_float_extensions (sub);
101 /* Convert EXPR to some floating-point type TYPE.
103 EXPR must be float, integer, or enumeral;
104 in other cases error is called. */
107 convert_to_real (type, expr)
110 enum built_in_function fcode = builtin_mathfn_code (expr);
111 tree itype = TREE_TYPE (expr);
113 /* Disable until we figure out how to decide whether the functions are
114 present in runtime. */
116 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
117 if ((fcode == BUILT_IN_SQRT
118 || fcode == BUILT_IN_SQRTL
119 || fcode == BUILT_IN_SIN
120 || fcode == BUILT_IN_SINL
121 || fcode == BUILT_IN_COS
122 || fcode == BUILT_IN_COSL
123 || fcode == BUILT_IN_EXP
124 || fcode == BUILT_IN_EXPL)
126 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
127 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
129 tree arg0 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
132 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
133 the both as the safe type for operation. */
134 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
135 newtype = TREE_TYPE (arg0);
137 /* Be curefull about integer to fp conversions.
138 These may overflow still. */
139 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
140 && TYPE_PRECISION (newtype) <= TYPE_PRECISION (itype)
141 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
142 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
145 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
150 fcode = BUILT_IN_SQRTF;
154 fcode = BUILT_IN_SINF;
158 fcode = BUILT_IN_COSF;
162 fcode = BUILT_IN_EXPF;
172 fcode = BUILT_IN_SQRT;
176 fcode = BUILT_IN_SIN;
180 fcode = BUILT_IN_COS;
184 fcode = BUILT_IN_EXP;
190 /* ??? Fortran frontend does not initialize built_in_decls.
191 For some reason creating the decl using builtin_function does not
192 work as it should. */
193 if (built_in_decls [fcode])
195 arglist = build_tree_list (NULL_TREE, fold (convert_to_real (newtype, arg0)));
196 expr = build_function_call_expr (built_in_decls [fcode], arglist);
204 /* Propagate the cast into the operation. */
205 if (itype != type && FLOAT_TYPE_P (type))
206 switch (TREE_CODE (expr))
208 /* convert (float)-x into -(float)x. This is always safe. */
211 return build1 (TREE_CODE (expr), type,
212 fold (convert_to_real (type,
213 TREE_OPERAND (expr, 0))));
214 /* convert (outertype)((innertype0)a+(innertype1)b)
215 into ((newtype)a+(newtype)b) where newtype
216 is the widest mode from all of these. */
222 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
223 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
225 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
226 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
229 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
230 newtype = TREE_TYPE (arg0);
231 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
232 newtype = TREE_TYPE (arg1);
233 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
235 expr = build (TREE_CODE (expr), newtype,
236 fold (convert_to_real (newtype, arg0)),
237 fold (convert_to_real (newtype, arg1)));
248 switch (TREE_CODE (TREE_TYPE (expr)))
251 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
258 return build1 (FLOAT_EXPR, type, expr);
261 return convert (type,
262 fold (build1 (REALPART_EXPR,
263 TREE_TYPE (TREE_TYPE (expr)), expr)));
267 error ("pointer value used where a floating point value was expected");
268 return convert_to_real (type, integer_zero_node);
271 error ("aggregate value used where a float was expected");
272 return convert_to_real (type, integer_zero_node);
276 /* Convert EXPR to some integer (or enum) type TYPE.
278 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
279 vector; in other cases error is called.
281 The result of this is always supposed to be a newly created tree node
282 not in use in any existing structure. */
285 convert_to_integer (type, expr)
288 enum tree_code ex_form = TREE_CODE (expr);
289 tree intype = TREE_TYPE (expr);
290 unsigned int inprec = TYPE_PRECISION (intype);
291 unsigned int outprec = TYPE_PRECISION (type);
293 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
294 be. Consider `enum E = { a, b = (enum E) 3 };'. */
295 if (!COMPLETE_TYPE_P (type))
297 error ("conversion to incomplete type");
298 return error_mark_node;
301 switch (TREE_CODE (intype))
305 if (integer_zerop (expr))
306 expr = integer_zero_node;
308 expr = fold (build1 (CONVERT_EXPR, (*lang_hooks.types.type_for_size)
309 (POINTER_SIZE, 0), expr));
311 return convert_to_integer (type, expr);
317 /* If this is a logical operation, which just returns 0 or 1, we can
318 change the type of the expression. For some logical operations,
319 we must also change the types of the operands to maintain type
322 if (TREE_CODE_CLASS (ex_form) == '<')
324 TREE_TYPE (expr) = type;
328 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
329 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
330 || ex_form == TRUTH_XOR_EXPR)
332 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
333 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
334 TREE_TYPE (expr) = type;
338 else if (ex_form == TRUTH_NOT_EXPR)
340 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
341 TREE_TYPE (expr) = type;
345 /* If we are widening the type, put in an explicit conversion.
346 Similarly if we are not changing the width. After this, we know
347 we are truncating EXPR. */
349 else if (outprec >= inprec)
350 return build1 (NOP_EXPR, type, expr);
352 /* If TYPE is an enumeral type or a type with a precision less
353 than the number of bits in its mode, do the conversion to the
354 type corresponding to its mode, then do a nop conversion
356 else if (TREE_CODE (type) == ENUMERAL_TYPE
357 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
358 return build1 (NOP_EXPR, type,
359 convert ((*lang_hooks.types.type_for_mode)
360 (TYPE_MODE (type), TREE_UNSIGNED (type)),
363 /* Here detect when we can distribute the truncation down past some
364 arithmetic. For example, if adding two longs and converting to an
365 int, we can equally well convert both to ints and then add.
366 For the operations handled here, such truncation distribution
368 It is desirable in these cases:
369 1) when truncating down to full-word from a larger size
370 2) when truncating takes no work.
371 3) when at least one operand of the arithmetic has been extended
372 (as by C's default conversions). In this case we need two conversions
373 if we do the arithmetic as already requested, so we might as well
374 truncate both and then combine. Perhaps that way we need only one.
376 Note that in general we cannot do the arithmetic in a type
377 shorter than the desired result of conversion, even if the operands
378 are both extended from a shorter type, because they might overflow
379 if combined in that type. The exceptions to this--the times when
380 two narrow values can be combined in their narrow type even to
381 make a wider result--are handled by "shorten" in build_binary_op. */
386 /* We can pass truncation down through right shifting
387 when the shift count is a nonpositive constant. */
388 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
389 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
390 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
396 /* We can pass truncation down through left shifting
397 when the shift count is a nonnegative constant and
398 the target type is unsigned. */
399 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
400 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
401 && TREE_UNSIGNED (type)
402 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
404 /* If shift count is less than the width of the truncated type,
406 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
407 /* In this case, shifting is like multiplication. */
411 /* If it is >= that width, result is zero.
412 Handling this with trunc1 would give the wrong result:
413 (int) ((long long) a << 32) is well defined (as 0)
414 but (int) a << 32 is undefined and would get a
417 tree t = convert_to_integer (type, integer_zero_node);
419 /* If the original expression had side-effects, we must
421 if (TREE_SIDE_EFFECTS (expr))
422 return build (COMPOUND_EXPR, type, expr, t);
433 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
434 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
436 /* Don't distribute unless the output precision is at least as big
437 as the actual inputs. Otherwise, the comparison of the
438 truncated values will be wrong. */
439 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
440 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
441 /* If signedness of arg0 and arg1 don't match,
442 we can't necessarily find a type to compare them in. */
443 && (TREE_UNSIGNED (TREE_TYPE (arg0))
444 == TREE_UNSIGNED (TREE_TYPE (arg1))))
457 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
458 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
460 if (outprec >= BITS_PER_WORD
461 || TRULY_NOOP_TRUNCATION (outprec, inprec)
462 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
463 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
465 /* Do the arithmetic in type TYPEX,
466 then convert result to TYPE. */
469 /* Can't do arithmetic in enumeral types
470 so use an integer type that will hold the values. */
471 if (TREE_CODE (typex) == ENUMERAL_TYPE)
472 typex = (*lang_hooks.types.type_for_size)
473 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
475 /* But now perhaps TYPEX is as wide as INPREC.
476 In that case, do nothing special here.
477 (Otherwise would recurse infinitely in convert. */
478 if (TYPE_PRECISION (typex) != inprec)
480 /* Don't do unsigned arithmetic where signed was wanted,
482 Exception: if both of the original operands were
483 unsigned then we can safely do the work as unsigned.
484 Exception: shift operations take their type solely
485 from the first argument.
486 Exception: the LSHIFT_EXPR case above requires that
487 we perform this operation unsigned lest we produce
488 signed-overflow undefinedness.
489 And we may need to do it as unsigned
490 if we truncate to the original size. */
491 if (TREE_UNSIGNED (TREE_TYPE (expr))
492 || (TREE_UNSIGNED (TREE_TYPE (arg0))
493 && (TREE_UNSIGNED (TREE_TYPE (arg1))
494 || ex_form == LSHIFT_EXPR
495 || ex_form == RSHIFT_EXPR
496 || ex_form == LROTATE_EXPR
497 || ex_form == RROTATE_EXPR))
498 || ex_form == LSHIFT_EXPR)
499 typex = (*lang_hooks.types.unsigned_type) (typex);
501 typex = (*lang_hooks.types.signed_type) (typex);
502 return convert (type,
503 fold (build (ex_form, typex,
504 convert (typex, arg0),
505 convert (typex, arg1),
514 /* This is not correct for ABS_EXPR,
515 since we must test the sign before truncation. */
519 /* Can't do arithmetic in enumeral types
520 so use an integer type that will hold the values. */
521 if (TREE_CODE (typex) == ENUMERAL_TYPE)
522 typex = (*lang_hooks.types.type_for_size)
523 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
525 /* But now perhaps TYPEX is as wide as INPREC.
526 In that case, do nothing special here.
527 (Otherwise would recurse infinitely in convert. */
528 if (TYPE_PRECISION (typex) != inprec)
530 /* Don't do unsigned arithmetic where signed was wanted,
532 if (TREE_UNSIGNED (TREE_TYPE (expr)))
533 typex = (*lang_hooks.types.unsigned_type) (typex);
535 typex = (*lang_hooks.types.signed_type) (typex);
536 return convert (type,
537 fold (build1 (ex_form, typex,
539 TREE_OPERAND (expr, 0)))));
545 "can't convert between vector values of different size" error. */
546 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
547 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
548 != GET_MODE_SIZE (TYPE_MODE (type))))
550 /* If truncating after truncating, might as well do all at once.
551 If truncating after extending, we may get rid of wasted work. */
552 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
555 /* It is sometimes worthwhile to push the narrowing down through
556 the conditional and never loses. */
557 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
558 convert (type, TREE_OPERAND (expr, 1)),
559 convert (type, TREE_OPERAND (expr, 2))));
565 return build1 (NOP_EXPR, type, expr);
568 return build1 (FIX_TRUNC_EXPR, type, expr);
571 return convert (type,
572 fold (build1 (REALPART_EXPR,
573 TREE_TYPE (TREE_TYPE (expr)), expr)));
576 if (GET_MODE_SIZE (TYPE_MODE (type))
577 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
579 error ("can't convert between vector values of different size");
580 return error_mark_node;
582 return build1 (NOP_EXPR, type, expr);
585 error ("aggregate value used where an integer was expected");
586 return convert (type, integer_zero_node);
590 /* Convert EXPR to the complex type TYPE in the usual ways. */
593 convert_to_complex (type, expr)
596 tree subtype = TREE_TYPE (type);
598 switch (TREE_CODE (TREE_TYPE (expr)))
605 return build (COMPLEX_EXPR, type, convert (subtype, expr),
606 convert (subtype, integer_zero_node));
610 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
612 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
614 else if (TREE_CODE (expr) == COMPLEX_EXPR)
615 return fold (build (COMPLEX_EXPR,
617 convert (subtype, TREE_OPERAND (expr, 0)),
618 convert (subtype, TREE_OPERAND (expr, 1))));
621 expr = save_expr (expr);
623 fold (build (COMPLEX_EXPR,
624 type, convert (subtype,
625 fold (build1 (REALPART_EXPR,
626 TREE_TYPE (TREE_TYPE (expr)),
629 fold (build1 (IMAGPART_EXPR,
630 TREE_TYPE (TREE_TYPE (expr)),
637 error ("pointer value used where a complex was expected");
638 return convert_to_complex (type, integer_zero_node);
641 error ("aggregate value used where a complex was expected");
642 return convert_to_complex (type, integer_zero_node);
646 /* Convert EXPR to the vector type TYPE in the usual ways. */
649 convert_to_vector (type, expr)
652 switch (TREE_CODE (TREE_TYPE (expr)))
656 if (GET_MODE_SIZE (TYPE_MODE (type))
657 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
659 error ("can't convert between vector values of different size");
660 return error_mark_node;
662 return build1 (NOP_EXPR, type, expr);
665 error ("can't convert value to a vector");
666 return convert_to_vector (type, integer_zero_node);