1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 88, 91-95, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU C.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* These routines are somewhat language-independent utility function
23 intended to be called by the language-specific convert () functions. */
32 /* Convert EXPR to some pointer or reference type TYPE.
34 EXPR must be pointer, reference, integer, enumeral, or literal zero;
35 in other cases error is called. */
38 convert_to_pointer (type
, expr
)
41 if (integer_zerop (expr
))
43 expr
= build_int_2 (0, 0);
44 TREE_TYPE (expr
) = type
;
48 switch (TREE_CODE (TREE_TYPE (expr
)))
52 return build1 (NOP_EXPR
, type
, expr
);
58 if (TYPE_PRECISION (TREE_TYPE (expr
)) == POINTER_SIZE
)
59 return build1 (CONVERT_EXPR
, type
, expr
);
62 convert_to_pointer (type
,
63 convert (type_for_size (POINTER_SIZE
, 0), expr
));
66 error ("cannot convert to a pointer type");
67 return convert_to_pointer (type
, integer_zero_node
);
71 /* Convert EXPR to some floating-point type TYPE.
73 EXPR must be float, integer, or enumeral;
74 in other cases error is called. */
77 convert_to_real (type
, expr
)
80 switch (TREE_CODE (TREE_TYPE (expr
)))
83 return build1 (flag_float_store
? CONVERT_EXPR
: NOP_EXPR
,
90 return build1 (FLOAT_EXPR
, type
, expr
);
94 fold (build1 (REALPART_EXPR
,
95 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
99 error ("pointer value used where a floating point value was expected");
100 return convert_to_real (type
, integer_zero_node
);
103 error ("aggregate value used where a float was expected");
104 return convert_to_real (type
, integer_zero_node
);
108 /* Convert EXPR to some integer (or enum) type TYPE.
110 EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
111 in other cases error is called.
113 The result of this is always supposed to be a newly created tree node
114 not in use in any existing structure. */
117 convert_to_integer (type
, expr
)
120 enum tree_code ex_form
= TREE_CODE (expr
);
121 tree intype
= TREE_TYPE (expr
);
122 int inprec
= TYPE_PRECISION (intype
);
123 int outprec
= TYPE_PRECISION (type
);
125 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
126 be. Consider `enum E = { a, b = (enum E) 3 };'. */
127 if (!TYPE_SIZE (type
))
129 error ("conversion to incomplete type");
130 return error_mark_node
;
133 switch (TREE_CODE (intype
))
137 if (integer_zerop (expr
))
138 expr
= integer_zero_node
;
140 expr
= fold (build1 (CONVERT_EXPR
,
141 type_for_size (POINTER_SIZE
, 0), expr
));
143 return convert_to_integer (type
, expr
);
149 /* If this is a logical operation, which just returns 0 or 1, we can
150 change the type of the expression. For some logical operations,
151 we must also change the types of the operands to maintain type
154 if (TREE_CODE_CLASS (ex_form
) == '<')
156 TREE_TYPE (expr
) = type
;
160 else if (ex_form
== TRUTH_AND_EXPR
|| ex_form
== TRUTH_ANDIF_EXPR
161 || ex_form
== TRUTH_OR_EXPR
|| ex_form
== TRUTH_ORIF_EXPR
162 || ex_form
== TRUTH_XOR_EXPR
)
164 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
165 TREE_OPERAND (expr
, 1) = convert (type
, TREE_OPERAND (expr
, 1));
166 TREE_TYPE (expr
) = type
;
170 else if (ex_form
== TRUTH_NOT_EXPR
)
172 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
173 TREE_TYPE (expr
) = type
;
177 /* If we are widening the type, put in an explicit conversion.
178 Similarly if we are not changing the width. After this, we know
179 we are truncating EXPR. */
181 else if (outprec
>= inprec
)
182 return build1 (NOP_EXPR
, type
, expr
);
184 /* If TYPE is an enumeral type or a type with a precision less
185 than the number of bits in its mode, do the conversion to the
186 type corresponding to its mode, then do a nop conversion
188 else if (TREE_CODE (type
) == ENUMERAL_TYPE
189 || outprec
!= GET_MODE_BITSIZE (TYPE_MODE (type
)))
190 return build1 (NOP_EXPR
, type
,
191 convert (type_for_mode (TYPE_MODE (type
),
192 TREE_UNSIGNED (type
)),
195 /* Here detect when we can distribute the truncation down past some
196 arithmetic. For example, if adding two longs and converting to an
197 int, we can equally well convert both to ints and then add.
198 For the operations handled here, such truncation distribution
200 It is desirable in these cases:
201 1) when truncating down to full-word from a larger size
202 2) when truncating takes no work.
203 3) when at least one operand of the arithmetic has been extended
204 (as by C's default conversions). In this case we need two conversions
205 if we do the arithmetic as already requested, so we might as well
206 truncate both and then combine. Perhaps that way we need only one.
208 Note that in general we cannot do the arithmetic in a type
209 shorter than the desired result of conversion, even if the operands
210 are both extended from a shorter type, because they might overflow
211 if combined in that type. The exceptions to this--the times when
212 two narrow values can be combined in their narrow type even to
213 make a wider result--are handled by "shorten" in build_binary_op. */
218 /* We can pass truncation down through right shifting
219 when the shift count is a nonpositive constant. */
220 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
221 && tree_int_cst_lt (TREE_OPERAND (expr
, 1),
222 convert (TREE_TYPE (TREE_OPERAND (expr
, 1)),
228 /* We can pass truncation down through left shifting
229 when the shift count is a nonnegative constant. */
230 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
231 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
232 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
234 /* If shift count is less than the width of the truncated type,
236 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
237 /* In this case, shifting is like multiplication. */
241 /* If it is >= that width, result is zero.
242 Handling this with trunc1 would give the wrong result:
243 (int) ((long long) a << 32) is well defined (as 0)
244 but (int) a << 32 is undefined and would get a
247 tree t
= convert_to_integer (type
, integer_zero_node
);
249 /* If the original expression had side-effects, we must
251 if (TREE_SIDE_EFFECTS (expr
))
252 return build (COMPOUND_EXPR
, type
, expr
, t
);
263 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
264 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
266 /* Don't distribute unless the output precision is at least as big
267 as the actual inputs. Otherwise, the comparison of the
268 truncated values will be wrong. */
269 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
270 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
271 /* If signedness of arg0 and arg1 don't match,
272 we can't necessarily find a type to compare them in. */
273 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
274 == TREE_UNSIGNED (TREE_TYPE (arg1
))))
287 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
288 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
290 if (outprec
>= BITS_PER_WORD
291 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
292 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
293 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
295 /* Do the arithmetic in type TYPEX,
296 then convert result to TYPE. */
297 register tree typex
= type
;
299 /* Can't do arithmetic in enumeral types
300 so use an integer type that will hold the values. */
301 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
302 typex
= type_for_size (TYPE_PRECISION (typex
),
303 TREE_UNSIGNED (typex
));
305 /* But now perhaps TYPEX is as wide as INPREC.
306 In that case, do nothing special here.
307 (Otherwise would recurse infinitely in convert. */
308 if (TYPE_PRECISION (typex
) != inprec
)
310 /* Don't do unsigned arithmetic where signed was wanted,
312 Exception: if either of the original operands were
313 unsigned then can safely do the work as unsigned.
314 And we may need to do it as unsigned
315 if we truncate to the original size. */
316 typex
= ((TREE_UNSIGNED (TREE_TYPE (expr
))
317 || TREE_UNSIGNED (TREE_TYPE (arg0
))
318 || TREE_UNSIGNED (TREE_TYPE (arg1
)))
319 ? unsigned_type (typex
) : signed_type (typex
));
320 return convert (type
,
321 fold (build (ex_form
, typex
,
322 convert (typex
, arg0
),
323 convert (typex
, arg1
),
332 /* This is not correct for ABS_EXPR,
333 since we must test the sign before truncation. */
335 register tree typex
= type
;
337 /* Can't do arithmetic in enumeral types
338 so use an integer type that will hold the values. */
339 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
340 typex
= type_for_size (TYPE_PRECISION (typex
),
341 TREE_UNSIGNED (typex
));
343 /* But now perhaps TYPEX is as wide as INPREC.
344 In that case, do nothing special here.
345 (Otherwise would recurse infinitely in convert. */
346 if (TYPE_PRECISION (typex
) != inprec
)
348 /* Don't do unsigned arithmetic where signed was wanted,
350 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
351 ? unsigned_type (typex
) : signed_type (typex
));
352 return convert (type
,
353 fold (build1 (ex_form
, typex
,
355 TREE_OPERAND (expr
, 0)))));
360 /* If truncating after truncating, might as well do all at once.
361 If truncating after extending, we may get rid of wasted work. */
362 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
365 /* It is sometimes worthwhile to push the narrowing down through
366 the conditional and never loses. */
367 return fold (build (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
368 convert (type
, TREE_OPERAND (expr
, 1)),
369 convert (type
, TREE_OPERAND (expr
, 2))));
375 return build1 (NOP_EXPR
, type
, expr
);
378 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
381 return convert (type
,
382 fold (build1 (REALPART_EXPR
,
383 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
386 error ("aggregate value used where an integer was expected");
387 return convert (type
, integer_zero_node
);
391 /* Convert EXPR to the complex type TYPE in the usual ways. */
394 convert_to_complex (type
, expr
)
397 tree subtype
= TREE_TYPE (type
);
399 switch (TREE_CODE (TREE_TYPE (expr
)))
406 return build (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
407 convert (subtype
, integer_zero_node
));
411 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
413 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
415 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
416 return fold (build (COMPLEX_EXPR
,
418 convert (subtype
, TREE_OPERAND (expr
, 0)),
419 convert (subtype
, TREE_OPERAND (expr
, 1))));
422 expr
= save_expr (expr
);
424 fold (build (COMPLEX_EXPR
,
425 type
, convert (subtype
,
426 fold (build1 (REALPART_EXPR
,
427 TREE_TYPE (TREE_TYPE (expr
)),
430 fold (build1 (IMAGPART_EXPR
,
431 TREE_TYPE (TREE_TYPE (expr
)),
438 error ("pointer value used where a complex was expected");
439 return convert_to_complex (type
, integer_zero_node
);
442 error ("aggregate value used where a complex was expected");
443 return convert_to_complex (type
, integer_zero_node
);