1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 1988, 1991, 1992, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* These routines are somewhat language-independent utility function
22 intended to be called by the language-specific convert () functions. */
29 /* Convert EXPR to some pointer type TYPE.
31 EXPR must be pointer, integer, enumeral, or literal zero;
32 in other cases error is called. */
35 convert_to_pointer (type
, expr
)
38 register tree intype
= TREE_TYPE (expr
);
39 register enum tree_code form
= TREE_CODE (intype
);
41 if (integer_zerop (expr
))
43 if (type
== TREE_TYPE (null_pointer_node
))
44 return null_pointer_node
;
45 expr
= build_int_2 (0, 0);
46 TREE_TYPE (expr
) = type
;
50 if (form
== POINTER_TYPE
)
51 return build1 (NOP_EXPR
, type
, expr
);
54 if (form
== INTEGER_TYPE
|| form
== ENUMERAL_TYPE
)
56 if (type_precision (intype
) == POINTER_SIZE
)
57 return build1 (CONVERT_EXPR
, type
, expr
);
58 expr
= convert (type_for_size (POINTER_SIZE
, 0), expr
);
59 /* Modes may be different but sizes should be the same. */
60 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
61 != GET_MODE_SIZE (TYPE_MODE (type
)))
62 /* There is supposed to be some integral type
63 that is the same width as a pointer. */
65 return convert_to_pointer (type
, expr
);
68 error ("cannot convert to a pointer type");
70 return null_pointer_node
;
73 /* Convert EXPR to some floating-point type TYPE.
75 EXPR must be float, integer, or enumeral;
76 in other cases error is called. */
79 convert_to_real (type
, expr
)
82 register enum tree_code form
= TREE_CODE (TREE_TYPE (expr
));
84 if (form
== REAL_TYPE
)
85 return build1 (flag_float_store
? CONVERT_EXPR
: NOP_EXPR
,
88 if (INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
89 return build1 (FLOAT_EXPR
, type
, expr
);
91 if (form
== COMPLEX_TYPE
)
92 return convert (type
, fold (build1 (REALPART_EXPR
,
93 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
95 if (form
== POINTER_TYPE
)
96 error ("pointer value used where a floating point value was expected");
98 error ("aggregate value used where a float was expected");
101 register tree tem
= make_node (REAL_CST
);
102 TREE_TYPE (tem
) = type
;
103 TREE_REAL_CST (tem
) = REAL_VALUE_ATOF ("0.0", TYPE_MODE (type
));
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 register tree intype
= TREE_TYPE (expr
);
121 register enum tree_code form
= TREE_CODE (intype
);
123 if (form
== POINTER_TYPE
)
125 if (integer_zerop (expr
))
126 expr
= integer_zero_node
;
128 expr
= fold (build1 (CONVERT_EXPR
,
129 type_for_size (POINTER_SIZE
, 0), expr
));
130 intype
= TREE_TYPE (expr
);
131 form
= TREE_CODE (intype
);
136 if (form
== INTEGER_TYPE
|| form
== ENUMERAL_TYPE
137 || form
== BOOLEAN_TYPE
|| form
== CHAR_TYPE
)
139 register unsigned outprec
= TYPE_PRECISION (type
);
140 register unsigned inprec
= TYPE_PRECISION (intype
);
141 register enum tree_code ex_form
= TREE_CODE (expr
);
143 /* If we are widening the type, put in an explicit conversion.
144 Similarly if we are not changing the width. However, if this is
145 a logical operation that just returns 0 or 1, we can change the
146 type of the expression. For logical operations, we must
147 also change the types of the operands to maintain type
150 if (TREE_CODE_CLASS (ex_form
) == '<')
152 TREE_TYPE (expr
) = type
;
155 else if (ex_form
== TRUTH_AND_EXPR
|| ex_form
== TRUTH_ANDIF_EXPR
156 || ex_form
== TRUTH_OR_EXPR
|| ex_form
== TRUTH_ORIF_EXPR
157 || ex_form
== TRUTH_XOR_EXPR
)
159 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
160 TREE_OPERAND (expr
, 1) = convert (type
, TREE_OPERAND (expr
, 1));
161 TREE_TYPE (expr
) = type
;
164 else if (ex_form
== TRUTH_NOT_EXPR
)
166 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
167 TREE_TYPE (expr
) = type
;
170 else if (outprec
>= inprec
)
171 return build1 (NOP_EXPR
, type
, expr
);
173 /* Here detect when we can distribute the truncation down past some
174 arithmetic. For example, if adding two longs and converting to an
175 int, we can equally well convert both to ints and then add.
176 For the operations handled here, such truncation distribution
178 It is desirable in these cases:
179 1) when truncating down to full-word from a larger size
180 2) when truncating takes no work.
181 3) when at least one operand of the arithmetic has been extended
182 (as by C's default conversions). In this case we need two conversions
183 if we do the arithmetic as already requested, so we might as well
184 truncate both and then combine. Perhaps that way we need only one.
186 Note that in general we cannot do the arithmetic in a type
187 shorter than the desired result of conversion, even if the operands
188 are both extended from a shorter type, because they might overflow
189 if combined in that type. The exceptions to this--the times when
190 two narrow values can be combined in their narrow type even to
191 make a wider result--are handled by "shorten" in build_binary_op. */
196 /* We can pass truncation down through right shifting
197 when the shift count is a nonpositive constant. */
198 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
199 && tree_int_cst_lt (TREE_OPERAND (expr
, 1),
200 convert (TREE_TYPE (TREE_OPERAND (expr
, 1)),
206 /* We can pass truncation down through left shifting
207 when the shift count is a nonnegative constant. */
208 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
209 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
210 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
212 /* If shift count is less than the width of the truncated type,
214 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
215 /* In this case, shifting is like multiplication. */
219 /* If it is >= that width, result is zero.
220 Handling this with trunc1 would give the wrong result:
221 (int) ((long long) a << 32) is well defined (as 0)
222 but (int) a << 32 is undefined and would get a
225 tree t
= convert_to_integer (type
, integer_zero_node
);
227 /* If the original expression had side-effects, we must
229 if (TREE_SIDE_EFFECTS (expr
))
230 return build (COMPOUND_EXPR
, type
, expr
, t
);
241 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
242 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
244 /* Don't distribute unless the output precision is at least as big
245 as the actual inputs. Otherwise, the comparison of the
246 truncated values will be wrong. */
247 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
248 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
249 /* If signedness of arg0 and arg1 don't match,
250 we can't necessarily find a type to compare them in. */
251 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
252 == TREE_UNSIGNED (TREE_TYPE (arg1
))))
265 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
266 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
268 if (outprec
>= BITS_PER_WORD
269 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
270 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
271 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
273 /* Do the arithmetic in type TYPEX,
274 then convert result to TYPE. */
275 register tree typex
= type
;
277 /* Can't do arithmetic in enumeral types
278 so use an integer type that will hold the values. */
279 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
280 typex
= type_for_size (TYPE_PRECISION (typex
),
281 TREE_UNSIGNED (typex
));
283 /* But now perhaps TYPEX is as wide as INPREC.
284 In that case, do nothing special here.
285 (Otherwise would recurse infinitely in convert. */
286 if (TYPE_PRECISION (typex
) != inprec
)
288 /* Don't do unsigned arithmetic where signed was wanted,
290 Exception: if either of the original operands were
291 unsigned then can safely do the work as unsigned.
292 And we may need to do it as unsigned
293 if we truncate to the original size. */
294 typex
= ((TREE_UNSIGNED (TREE_TYPE (expr
))
295 || TREE_UNSIGNED (TREE_TYPE (arg0
))
296 || TREE_UNSIGNED (TREE_TYPE (arg1
)))
297 ? unsigned_type (typex
) : signed_type (typex
));
298 return convert (type
,
299 fold (build (ex_form
, typex
,
300 convert (typex
, arg0
),
301 convert (typex
, arg1
),
310 /* This is not correct for ABS_EXPR,
311 since we must test the sign before truncation. */
313 register tree typex
= type
;
315 /* Can't do arithmetic in enumeral types
316 so use an integer type that will hold the values. */
317 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
318 typex
= type_for_size (TYPE_PRECISION (typex
),
319 TREE_UNSIGNED (typex
));
321 /* But now perhaps TYPEX is as wide as INPREC.
322 In that case, do nothing special here.
323 (Otherwise would recurse infinitely in convert. */
324 if (TYPE_PRECISION (typex
) != inprec
)
326 /* Don't do unsigned arithmetic where signed was wanted,
328 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
329 ? unsigned_type (typex
) : signed_type (typex
));
330 return convert (type
,
331 fold (build1 (ex_form
, typex
,
333 TREE_OPERAND (expr
, 0)))));
338 /* If truncating after truncating, might as well do all at once.
339 If truncating after extending, we may get rid of wasted work. */
340 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
343 /* Can treat the two alternative values like the operands
344 of an arithmetic expression. */
346 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
347 tree arg2
= get_unwidened (TREE_OPERAND (expr
, 2), type
);
349 if (outprec
>= BITS_PER_WORD
350 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
351 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
))
352 || inprec
> TYPE_PRECISION (TREE_TYPE (arg2
)))
354 /* Do the arithmetic in type TYPEX,
355 then convert result to TYPE. */
356 register tree typex
= type
;
358 /* Can't do arithmetic in enumeral types
359 so use an integer type that will hold the values. */
360 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
361 typex
= type_for_size (TYPE_PRECISION (typex
),
362 TREE_UNSIGNED (typex
));
364 /* But now perhaps TYPEX is as wide as INPREC.
365 In that case, do nothing special here.
366 (Otherwise would recurse infinitely in convert. */
367 if (TYPE_PRECISION (typex
) != inprec
)
369 /* Don't do unsigned arithmetic where signed was wanted,
371 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
372 ? unsigned_type (typex
) : signed_type (typex
));
373 return convert (type
,
374 fold (build (COND_EXPR
, typex
,
375 TREE_OPERAND (expr
, 0),
376 convert (typex
, arg1
),
377 convert (typex
, arg2
))));
380 /* It is sometimes worthwhile
381 to push the narrowing down through the conditional. */
382 return fold (build (COND_EXPR
, type
,
383 TREE_OPERAND (expr
, 0),
384 convert (type
, TREE_OPERAND (expr
, 1)),
385 convert (type
, TREE_OPERAND (expr
, 2))));
391 return build1 (NOP_EXPR
, type
, expr
);
394 if (form
== REAL_TYPE
)
395 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
397 if (form
== COMPLEX_TYPE
)
398 return convert (type
, fold (build1 (REALPART_EXPR
,
399 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
401 error ("aggregate value used where an integer was expected");
404 register tree tem
= build_int_2 (0, 0);
405 TREE_TYPE (tem
) = type
;
410 /* Convert EXPR to the complex type TYPE in the usual ways. */
413 convert_to_complex (type
, expr
)
416 register enum tree_code form
= TREE_CODE (TREE_TYPE (expr
));
417 tree subtype
= TREE_TYPE (type
);
419 if (form
== REAL_TYPE
|| form
== INTEGER_TYPE
|| form
== ENUMERAL_TYPE
)
421 expr
= convert (subtype
, expr
);
422 return build (COMPLEX_EXPR
, type
, expr
,
423 convert (subtype
, integer_zero_node
));
426 if (form
== COMPLEX_TYPE
)
428 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
429 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
431 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
432 return fold (build (COMPLEX_EXPR
,
434 convert (subtype
, TREE_OPERAND (expr
, 0)),
435 convert (subtype
, TREE_OPERAND (expr
, 1))));
438 expr
= save_expr (expr
);
439 return fold (build (COMPLEX_EXPR
,
442 fold (build1 (REALPART_EXPR
,
443 TREE_TYPE (TREE_TYPE (expr
)),
446 fold (build1 (IMAGPART_EXPR
,
447 TREE_TYPE (TREE_TYPE (expr
)),
452 if (form
== POINTER_TYPE
)
453 error ("pointer value used where a complex was expected");
455 error ("aggregate value used where a complex was expected");
457 return build (COMPLEX_EXPR
, type
,
458 convert (subtype
, integer_zero_node
),
459 convert (subtype
, integer_zero_node
));