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 GNU C.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
33 /* Convert EXPR to some pointer or reference type TYPE.
35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
36 in other cases error is called. */
39 convert_to_pointer (type
, expr
)
42 if (integer_zerop (expr
))
44 expr
= build_int_2 (0, 0);
45 TREE_TYPE (expr
) = type
;
49 switch (TREE_CODE (TREE_TYPE (expr
)))
53 return build1 (NOP_EXPR
, type
, expr
);
59 if (TYPE_PRECISION (TREE_TYPE (expr
)) == POINTER_SIZE
)
60 return build1 (CONVERT_EXPR
, type
, expr
);
63 convert_to_pointer (type
,
64 convert (type_for_size (POINTER_SIZE
, 0), expr
));
67 error ("cannot convert to a pointer type");
68 return convert_to_pointer (type
, integer_zero_node
);
72 /* Convert EXPR to some floating-point type TYPE.
74 EXPR must be float, integer, or enumeral;
75 in other cases error is called. */
78 convert_to_real (type
, expr
)
81 switch (TREE_CODE (TREE_TYPE (expr
)))
84 return build1 (flag_float_store
? CONVERT_EXPR
: NOP_EXPR
,
91 return build1 (FLOAT_EXPR
, type
, expr
);
95 fold (build1 (REALPART_EXPR
,
96 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
100 error ("pointer value used where a floating point value was expected");
101 return convert_to_real (type
, integer_zero_node
);
104 error ("aggregate value used where a float was expected");
105 return convert_to_real (type
, integer_zero_node
);
109 /* Convert EXPR to some integer (or enum) type TYPE.
111 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
112 vector; in other cases error is called.
114 The result of this is always supposed to be a newly created tree node
115 not in use in any existing structure. */
118 convert_to_integer (type
, expr
)
121 enum tree_code ex_form
= TREE_CODE (expr
);
122 tree intype
= TREE_TYPE (expr
);
123 unsigned int inprec
= TYPE_PRECISION (intype
);
124 unsigned int outprec
= TYPE_PRECISION (type
);
126 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
127 be. Consider `enum E = { a, b = (enum E) 3 };'. */
128 if (!COMPLETE_TYPE_P (type
))
130 error ("conversion to incomplete type");
131 return error_mark_node
;
134 switch (TREE_CODE (intype
))
138 if (integer_zerop (expr
))
139 expr
= integer_zero_node
;
141 expr
= fold (build1 (CONVERT_EXPR
,
142 type_for_size (POINTER_SIZE
, 0), expr
));
144 return convert_to_integer (type
, expr
);
150 /* If this is a logical operation, which just returns 0 or 1, we can
151 change the type of the expression. For some logical operations,
152 we must also change the types of the operands to maintain type
155 if (TREE_CODE_CLASS (ex_form
) == '<')
157 TREE_TYPE (expr
) = type
;
161 else if (ex_form
== TRUTH_AND_EXPR
|| ex_form
== TRUTH_ANDIF_EXPR
162 || ex_form
== TRUTH_OR_EXPR
|| ex_form
== TRUTH_ORIF_EXPR
163 || ex_form
== TRUTH_XOR_EXPR
)
165 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
166 TREE_OPERAND (expr
, 1) = convert (type
, TREE_OPERAND (expr
, 1));
167 TREE_TYPE (expr
) = type
;
171 else if (ex_form
== TRUTH_NOT_EXPR
)
173 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
174 TREE_TYPE (expr
) = type
;
178 /* If we are widening the type, put in an explicit conversion.
179 Similarly if we are not changing the width. After this, we know
180 we are truncating EXPR. */
182 else if (outprec
>= inprec
)
183 return build1 (NOP_EXPR
, type
, expr
);
185 /* If TYPE is an enumeral type or a type with a precision less
186 than the number of bits in its mode, do the conversion to the
187 type corresponding to its mode, then do a nop conversion
189 else if (TREE_CODE (type
) == ENUMERAL_TYPE
190 || outprec
!= GET_MODE_BITSIZE (TYPE_MODE (type
)))
191 return build1 (NOP_EXPR
, type
,
192 convert (type_for_mode (TYPE_MODE (type
),
193 TREE_UNSIGNED (type
)),
196 /* Here detect when we can distribute the truncation down past some
197 arithmetic. For example, if adding two longs and converting to an
198 int, we can equally well convert both to ints and then add.
199 For the operations handled here, such truncation distribution
201 It is desirable in these cases:
202 1) when truncating down to full-word from a larger size
203 2) when truncating takes no work.
204 3) when at least one operand of the arithmetic has been extended
205 (as by C's default conversions). In this case we need two conversions
206 if we do the arithmetic as already requested, so we might as well
207 truncate both and then combine. Perhaps that way we need only one.
209 Note that in general we cannot do the arithmetic in a type
210 shorter than the desired result of conversion, even if the operands
211 are both extended from a shorter type, because they might overflow
212 if combined in that type. The exceptions to this--the times when
213 two narrow values can be combined in their narrow type even to
214 make a wider result--are handled by "shorten" in build_binary_op. */
219 /* We can pass truncation down through right shifting
220 when the shift count is a nonpositive constant. */
221 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
222 && tree_int_cst_lt (TREE_OPERAND (expr
, 1),
223 convert (TREE_TYPE (TREE_OPERAND (expr
, 1)),
229 /* We can pass truncation down through left shifting
230 when the shift count is a nonnegative constant. */
231 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
232 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
233 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
235 /* If shift count is less than the width of the truncated type,
237 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
238 /* In this case, shifting is like multiplication. */
242 /* If it is >= that width, result is zero.
243 Handling this with trunc1 would give the wrong result:
244 (int) ((long long) a << 32) is well defined (as 0)
245 but (int) a << 32 is undefined and would get a
248 tree t
= convert_to_integer (type
, integer_zero_node
);
250 /* If the original expression had side-effects, we must
252 if (TREE_SIDE_EFFECTS (expr
))
253 return build (COMPOUND_EXPR
, type
, expr
, t
);
264 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
265 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
267 /* Don't distribute unless the output precision is at least as big
268 as the actual inputs. Otherwise, the comparison of the
269 truncated values will be wrong. */
270 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
271 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
272 /* If signedness of arg0 and arg1 don't match,
273 we can't necessarily find a type to compare them in. */
274 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
275 == TREE_UNSIGNED (TREE_TYPE (arg1
))))
288 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
289 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
291 if (outprec
>= BITS_PER_WORD
292 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
293 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
294 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
296 /* Do the arithmetic in type TYPEX,
297 then convert result to TYPE. */
298 register tree typex
= type
;
300 /* Can't do arithmetic in enumeral types
301 so use an integer type that will hold the values. */
302 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
303 typex
= type_for_size (TYPE_PRECISION (typex
),
304 TREE_UNSIGNED (typex
));
306 /* But now perhaps TYPEX is as wide as INPREC.
307 In that case, do nothing special here.
308 (Otherwise would recurse infinitely in convert. */
309 if (TYPE_PRECISION (typex
) != inprec
)
311 /* Don't do unsigned arithmetic where signed was wanted,
313 Exception: if both of the original operands were
314 unsigned then can safely do the work as unsigned.
315 And we may need to do it as unsigned
316 if we truncate to the original size. */
317 typex
= ((TREE_UNSIGNED (TREE_TYPE (expr
))
318 || (TREE_UNSIGNED (TREE_TYPE (arg0
))
319 && TREE_UNSIGNED (TREE_TYPE (arg1
))))
320 ? unsigned_type (typex
) : signed_type (typex
));
321 return convert (type
,
322 fold (build (ex_form
, typex
,
323 convert (typex
, arg0
),
324 convert (typex
, arg1
),
333 /* This is not correct for ABS_EXPR,
334 since we must test the sign before truncation. */
336 register tree typex
= type
;
338 /* Can't do arithmetic in enumeral types
339 so use an integer type that will hold the values. */
340 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
341 typex
= type_for_size (TYPE_PRECISION (typex
),
342 TREE_UNSIGNED (typex
));
344 /* But now perhaps TYPEX is as wide as INPREC.
345 In that case, do nothing special here.
346 (Otherwise would recurse infinitely in convert. */
347 if (TYPE_PRECISION (typex
) != inprec
)
349 /* Don't do unsigned arithmetic where signed was wanted,
351 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
352 ? unsigned_type (typex
) : signed_type (typex
));
353 return convert (type
,
354 fold (build1 (ex_form
, typex
,
356 TREE_OPERAND (expr
, 0)))));
361 /* If truncating after truncating, might as well do all at once.
362 If truncating after extending, we may get rid of wasted work. */
363 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
366 /* It is sometimes worthwhile to push the narrowing down through
367 the conditional and never loses. */
368 return fold (build (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
369 convert (type
, TREE_OPERAND (expr
, 1)),
370 convert (type
, TREE_OPERAND (expr
, 2))));
376 return build1 (NOP_EXPR
, type
, expr
);
379 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
382 return convert (type
,
383 fold (build1 (REALPART_EXPR
,
384 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
387 if (GET_MODE_SIZE (TYPE_MODE (type
))
388 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
))))
390 error ("can't convert between vector values of different size");
391 return error_mark_node
;
393 return build1 (NOP_EXPR
, type
, expr
);
396 error ("aggregate value used where an integer was expected");
397 return convert (type
, integer_zero_node
);
401 /* Convert EXPR to the complex type TYPE in the usual ways. */
404 convert_to_complex (type
, expr
)
407 tree subtype
= TREE_TYPE (type
);
409 switch (TREE_CODE (TREE_TYPE (expr
)))
416 return build (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
417 convert (subtype
, integer_zero_node
));
421 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
423 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
425 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
426 return fold (build (COMPLEX_EXPR
,
428 convert (subtype
, TREE_OPERAND (expr
, 0)),
429 convert (subtype
, TREE_OPERAND (expr
, 1))));
432 expr
= save_expr (expr
);
434 fold (build (COMPLEX_EXPR
,
435 type
, convert (subtype
,
436 fold (build1 (REALPART_EXPR
,
437 TREE_TYPE (TREE_TYPE (expr
)),
440 fold (build1 (IMAGPART_EXPR
,
441 TREE_TYPE (TREE_TYPE (expr
)),
448 error ("pointer value used where a complex was expected");
449 return convert_to_complex (type
, integer_zero_node
);
452 error ("aggregate value used where a complex was expected");
453 return convert_to_complex (type
, integer_zero_node
);
457 /* Convert EXPR to the vector type TYPE in the usual ways. */
460 convert_to_vector (type
, expr
)
463 switch (TREE_CODE (TREE_TYPE (expr
)))
467 if (GET_MODE_SIZE (TYPE_MODE (type
))
468 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
))))
470 error ("can't convert between vector values of different size");
471 return error_mark_node
;
473 return build1 (NOP_EXPR
, type
, expr
);
476 error ("can't convert value to a vector");
477 return convert_to_vector (type
, integer_zero_node
);