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. */
31 /* Convert EXPR to some pointer or reference type TYPE.
33 EXPR must be pointer, reference, integer, enumeral, or literal zero;
34 in other cases error is called. */
37 convert_to_pointer (type
, expr
)
40 if (integer_zerop (expr
))
42 expr
= build_int_2 (0, 0);
43 TREE_TYPE (expr
) = type
;
47 switch (TREE_CODE (TREE_TYPE (expr
)))
51 return build1 (NOP_EXPR
, type
, expr
);
57 if (TYPE_PRECISION (TREE_TYPE (expr
)) == POINTER_SIZE
)
58 return build1 (CONVERT_EXPR
, type
, expr
);
61 convert_to_pointer (type
,
62 convert (type_for_size (POINTER_SIZE
, 0), expr
));
65 error ("cannot convert to a pointer type");
66 return convert_to_pointer (type
, integer_zero_node
);
70 /* Convert EXPR to some floating-point type TYPE.
72 EXPR must be float, integer, or enumeral;
73 in other cases error is called. */
76 convert_to_real (type
, expr
)
79 switch (TREE_CODE (TREE_TYPE (expr
)))
82 return build1 (flag_float_store
? CONVERT_EXPR
: NOP_EXPR
,
89 return build1 (FLOAT_EXPR
, type
, expr
);
93 fold (build1 (REALPART_EXPR
,
94 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
98 error ("pointer value used where a floating point value was expected");
99 return convert_to_real (type
, integer_zero_node
);
102 error ("aggregate value used where a float was expected");
103 return convert_to_real (type
, integer_zero_node
);
107 /* Convert EXPR to some integer (or enum) type TYPE.
109 EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
110 in other cases error is called.
112 The result of this is always supposed to be a newly created tree node
113 not in use in any existing structure. */
116 convert_to_integer (type
, expr
)
119 enum tree_code ex_form
= TREE_CODE (expr
);
120 tree intype
= TREE_TYPE (expr
);
121 int inprec
= TYPE_PRECISION (intype
);
122 int outprec
= TYPE_PRECISION (type
);
124 switch (TREE_CODE (intype
))
128 if (integer_zerop (expr
))
129 expr
= integer_zero_node
;
131 expr
= fold (build1 (CONVERT_EXPR
,
132 type_for_size (POINTER_SIZE
, 0), expr
));
134 return convert_to_integer (type
, expr
);
140 /* If this is a logical operation, which just returns 0 or 1, we can
141 change the type of the expression. For some logical operations,
142 we must also change the types of the operands to maintain type
145 if (TREE_CODE_CLASS (ex_form
) == '<')
147 TREE_TYPE (expr
) = type
;
151 else if (ex_form
== TRUTH_AND_EXPR
|| ex_form
== TRUTH_ANDIF_EXPR
152 || ex_form
== TRUTH_OR_EXPR
|| ex_form
== TRUTH_ORIF_EXPR
153 || ex_form
== TRUTH_XOR_EXPR
)
155 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
156 TREE_OPERAND (expr
, 1) = convert (type
, TREE_OPERAND (expr
, 1));
157 TREE_TYPE (expr
) = type
;
161 else if (ex_form
== TRUTH_NOT_EXPR
)
163 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
164 TREE_TYPE (expr
) = type
;
168 /* If we are widening the type, put in an explicit conversion.
169 Similarly if we are not changing the width. After this, we know
170 we are truncating EXPR. */
172 else if (outprec
>= inprec
)
173 return build1 (NOP_EXPR
, type
, expr
);
175 /* If TYPE is an enumeral type or a type with a precision less
176 than the number of bits in its mode, do the conversion to the
177 type corresponding to its mode, then do a nop conversion
179 else if (TREE_CODE (type
) == ENUMERAL_TYPE
180 || outprec
!= GET_MODE_BITSIZE (TYPE_MODE (type
)))
181 return build1 (NOP_EXPR
, type
,
182 convert (type_for_mode (TYPE_MODE (type
),
183 TREE_UNSIGNED (type
)),
186 /* Here detect when we can distribute the truncation down past some
187 arithmetic. For example, if adding two longs and converting to an
188 int, we can equally well convert both to ints and then add.
189 For the operations handled here, such truncation distribution
191 It is desirable in these cases:
192 1) when truncating down to full-word from a larger size
193 2) when truncating takes no work.
194 3) when at least one operand of the arithmetic has been extended
195 (as by C's default conversions). In this case we need two conversions
196 if we do the arithmetic as already requested, so we might as well
197 truncate both and then combine. Perhaps that way we need only one.
199 Note that in general we cannot do the arithmetic in a type
200 shorter than the desired result of conversion, even if the operands
201 are both extended from a shorter type, because they might overflow
202 if combined in that type. The exceptions to this--the times when
203 two narrow values can be combined in their narrow type even to
204 make a wider result--are handled by "shorten" in build_binary_op. */
209 /* We can pass truncation down through right shifting
210 when the shift count is a nonpositive constant. */
211 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
212 && tree_int_cst_lt (TREE_OPERAND (expr
, 1),
213 convert (TREE_TYPE (TREE_OPERAND (expr
, 1)),
219 /* We can pass truncation down through left shifting
220 when the shift count is a nonnegative constant. */
221 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
222 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
223 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
225 /* If shift count is less than the width of the truncated type,
227 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
228 /* In this case, shifting is like multiplication. */
232 /* If it is >= that width, result is zero.
233 Handling this with trunc1 would give the wrong result:
234 (int) ((long long) a << 32) is well defined (as 0)
235 but (int) a << 32 is undefined and would get a
238 tree t
= convert_to_integer (type
, integer_zero_node
);
240 /* If the original expression had side-effects, we must
242 if (TREE_SIDE_EFFECTS (expr
))
243 return build (COMPOUND_EXPR
, type
, expr
, t
);
254 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
255 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
257 /* Don't distribute unless the output precision is at least as big
258 as the actual inputs. Otherwise, the comparison of the
259 truncated values will be wrong. */
260 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
261 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
262 /* If signedness of arg0 and arg1 don't match,
263 we can't necessarily find a type to compare them in. */
264 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
265 == TREE_UNSIGNED (TREE_TYPE (arg1
))))
278 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
279 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
281 if (outprec
>= BITS_PER_WORD
282 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
283 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
284 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
286 /* Do the arithmetic in type TYPEX,
287 then convert result to TYPE. */
288 register tree typex
= type
;
290 /* Can't do arithmetic in enumeral types
291 so use an integer type that will hold the values. */
292 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
293 typex
= type_for_size (TYPE_PRECISION (typex
),
294 TREE_UNSIGNED (typex
));
296 /* But now perhaps TYPEX is as wide as INPREC.
297 In that case, do nothing special here.
298 (Otherwise would recurse infinitely in convert. */
299 if (TYPE_PRECISION (typex
) != inprec
)
301 /* Don't do unsigned arithmetic where signed was wanted,
303 Exception: if either of the original operands were
304 unsigned then can safely do the work as unsigned.
305 And we may need to do it as unsigned
306 if we truncate to the original size. */
307 typex
= ((TREE_UNSIGNED (TREE_TYPE (expr
))
308 || TREE_UNSIGNED (TREE_TYPE (arg0
))
309 || TREE_UNSIGNED (TREE_TYPE (arg1
)))
310 ? unsigned_type (typex
) : signed_type (typex
));
311 return convert (type
,
312 fold (build (ex_form
, typex
,
313 convert (typex
, arg0
),
314 convert (typex
, arg1
),
323 /* This is not correct for ABS_EXPR,
324 since we must test the sign before truncation. */
326 register tree typex
= type
;
328 /* Can't do arithmetic in enumeral types
329 so use an integer type that will hold the values. */
330 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
331 typex
= type_for_size (TYPE_PRECISION (typex
),
332 TREE_UNSIGNED (typex
));
334 /* But now perhaps TYPEX is as wide as INPREC.
335 In that case, do nothing special here.
336 (Otherwise would recurse infinitely in convert. */
337 if (TYPE_PRECISION (typex
) != inprec
)
339 /* Don't do unsigned arithmetic where signed was wanted,
341 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
342 ? unsigned_type (typex
) : signed_type (typex
));
343 return convert (type
,
344 fold (build1 (ex_form
, typex
,
346 TREE_OPERAND (expr
, 0)))));
351 /* If truncating after truncating, might as well do all at once.
352 If truncating after extending, we may get rid of wasted work. */
353 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
356 /* It is sometimes worthwhile to push the narrowing down through
357 the conditional and never loses. */
358 return fold (build (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
359 convert (type
, TREE_OPERAND (expr
, 1)),
360 convert (type
, TREE_OPERAND (expr
, 2))));
366 return build1 (NOP_EXPR
, type
, expr
);
369 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
372 return convert (type
,
373 fold (build1 (REALPART_EXPR
,
374 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
377 error ("aggregate value used where an integer was expected");
378 return convert (type
, integer_zero_node
);
382 /* Convert EXPR to the complex type TYPE in the usual ways. */
385 convert_to_complex (type
, expr
)
388 tree subtype
= TREE_TYPE (type
);
390 switch (TREE_CODE (TREE_TYPE (expr
)))
397 return build (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
398 convert (subtype
, integer_zero_node
));
402 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
404 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
406 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
407 return fold (build (COMPLEX_EXPR
,
409 convert (subtype
, TREE_OPERAND (expr
, 0)),
410 convert (subtype
, TREE_OPERAND (expr
, 1))));
413 expr
= save_expr (expr
);
415 fold (build (COMPLEX_EXPR
,
416 type
, convert (subtype
,
417 fold (build1 (REALPART_EXPR
,
418 TREE_TYPE (TREE_TYPE (expr
)),
421 fold (build1 (IMAGPART_EXPR
,
422 TREE_TYPE (TREE_TYPE (expr
)),
429 error ("pointer value used where a complex was expected");
430 return convert_to_complex (type
, integer_zero_node
);
433 error ("aggregate value used where a complex was expected");
434 return convert_to_complex (type
, integer_zero_node
);