]>
gcc.gnu.org Git - gcc.git/blob - gcc/convert.c
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 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
125 be. Consider `enum E = { a, b = (enum E) 3 };'. */
126 if (!TYPE_SIZE (type
))
128 error ("conversion to incomplete type");
129 return error_mark_node
;
132 switch (TREE_CODE (intype
))
136 if (integer_zerop (expr
))
137 expr
= integer_zero_node
;
139 expr
= fold (build1 (CONVERT_EXPR
,
140 type_for_size (POINTER_SIZE
, 0), expr
));
142 return convert_to_integer (type
, expr
);
148 /* If this is a logical operation, which just returns 0 or 1, we can
149 change the type of the expression. For some logical operations,
150 we must also change the types of the operands to maintain type
153 if (TREE_CODE_CLASS (ex_form
) == '<')
155 TREE_TYPE (expr
) = type
;
159 else if (ex_form
== TRUTH_AND_EXPR
|| ex_form
== TRUTH_ANDIF_EXPR
160 || ex_form
== TRUTH_OR_EXPR
|| ex_form
== TRUTH_ORIF_EXPR
161 || ex_form
== TRUTH_XOR_EXPR
)
163 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
164 TREE_OPERAND (expr
, 1) = convert (type
, TREE_OPERAND (expr
, 1));
165 TREE_TYPE (expr
) = type
;
169 else if (ex_form
== TRUTH_NOT_EXPR
)
171 TREE_OPERAND (expr
, 0) = convert (type
, TREE_OPERAND (expr
, 0));
172 TREE_TYPE (expr
) = type
;
176 /* If we are widening the type, put in an explicit conversion.
177 Similarly if we are not changing the width. After this, we know
178 we are truncating EXPR. */
180 else if (outprec
>= inprec
)
181 return build1 (NOP_EXPR
, type
, expr
);
183 /* If TYPE is an enumeral type or a type with a precision less
184 than the number of bits in its mode, do the conversion to the
185 type corresponding to its mode, then do a nop conversion
187 else if (TREE_CODE (type
) == ENUMERAL_TYPE
188 || outprec
!= GET_MODE_BITSIZE (TYPE_MODE (type
)))
189 return build1 (NOP_EXPR
, type
,
190 convert (type_for_mode (TYPE_MODE (type
),
191 TREE_UNSIGNED (type
)),
194 /* Here detect when we can distribute the truncation down past some
195 arithmetic. For example, if adding two longs and converting to an
196 int, we can equally well convert both to ints and then add.
197 For the operations handled here, such truncation distribution
199 It is desirable in these cases:
200 1) when truncating down to full-word from a larger size
201 2) when truncating takes no work.
202 3) when at least one operand of the arithmetic has been extended
203 (as by C's default conversions). In this case we need two conversions
204 if we do the arithmetic as already requested, so we might as well
205 truncate both and then combine. Perhaps that way we need only one.
207 Note that in general we cannot do the arithmetic in a type
208 shorter than the desired result of conversion, even if the operands
209 are both extended from a shorter type, because they might overflow
210 if combined in that type. The exceptions to this--the times when
211 two narrow values can be combined in their narrow type even to
212 make a wider result--are handled by "shorten" in build_binary_op. */
217 /* We can pass truncation down through right shifting
218 when the shift count is a nonpositive constant. */
219 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
220 && tree_int_cst_lt (TREE_OPERAND (expr
, 1),
221 convert (TREE_TYPE (TREE_OPERAND (expr
, 1)),
227 /* We can pass truncation down through left shifting
228 when the shift count is a nonnegative constant. */
229 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
230 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
231 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
233 /* If shift count is less than the width of the truncated type,
235 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
236 /* In this case, shifting is like multiplication. */
240 /* If it is >= that width, result is zero.
241 Handling this with trunc1 would give the wrong result:
242 (int) ((long long) a << 32) is well defined (as 0)
243 but (int) a << 32 is undefined and would get a
246 tree t
= convert_to_integer (type
, integer_zero_node
);
248 /* If the original expression had side-effects, we must
250 if (TREE_SIDE_EFFECTS (expr
))
251 return build (COMPOUND_EXPR
, type
, expr
, t
);
262 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
263 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
265 /* Don't distribute unless the output precision is at least as big
266 as the actual inputs. Otherwise, the comparison of the
267 truncated values will be wrong. */
268 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
269 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
270 /* If signedness of arg0 and arg1 don't match,
271 we can't necessarily find a type to compare them in. */
272 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
273 == TREE_UNSIGNED (TREE_TYPE (arg1
))))
286 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
287 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
289 if (outprec
>= BITS_PER_WORD
290 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
291 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
292 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
294 /* Do the arithmetic in type TYPEX,
295 then convert result to TYPE. */
296 register tree typex
= type
;
298 /* Can't do arithmetic in enumeral types
299 so use an integer type that will hold the values. */
300 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
301 typex
= type_for_size (TYPE_PRECISION (typex
),
302 TREE_UNSIGNED (typex
));
304 /* But now perhaps TYPEX is as wide as INPREC.
305 In that case, do nothing special here.
306 (Otherwise would recurse infinitely in convert. */
307 if (TYPE_PRECISION (typex
) != inprec
)
309 /* Don't do unsigned arithmetic where signed was wanted,
311 Exception: if either of the original operands were
312 unsigned then can safely do the work as unsigned.
313 And we may need to do it as unsigned
314 if we truncate to the original size. */
315 typex
= ((TREE_UNSIGNED (TREE_TYPE (expr
))
316 || TREE_UNSIGNED (TREE_TYPE (arg0
))
317 || TREE_UNSIGNED (TREE_TYPE (arg1
)))
318 ? unsigned_type (typex
) : signed_type (typex
));
319 return convert (type
,
320 fold (build (ex_form
, typex
,
321 convert (typex
, arg0
),
322 convert (typex
, arg1
),
331 /* This is not correct for ABS_EXPR,
332 since we must test the sign before truncation. */
334 register tree typex
= type
;
336 /* Can't do arithmetic in enumeral types
337 so use an integer type that will hold the values. */
338 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
339 typex
= type_for_size (TYPE_PRECISION (typex
),
340 TREE_UNSIGNED (typex
));
342 /* But now perhaps TYPEX is as wide as INPREC.
343 In that case, do nothing special here.
344 (Otherwise would recurse infinitely in convert. */
345 if (TYPE_PRECISION (typex
) != inprec
)
347 /* Don't do unsigned arithmetic where signed was wanted,
349 typex
= (TREE_UNSIGNED (TREE_TYPE (expr
))
350 ? unsigned_type (typex
) : signed_type (typex
));
351 return convert (type
,
352 fold (build1 (ex_form
, typex
,
354 TREE_OPERAND (expr
, 0)))));
359 /* If truncating after truncating, might as well do all at once.
360 If truncating after extending, we may get rid of wasted work. */
361 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
364 /* It is sometimes worthwhile to push the narrowing down through
365 the conditional and never loses. */
366 return fold (build (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
367 convert (type
, TREE_OPERAND (expr
, 1)),
368 convert (type
, TREE_OPERAND (expr
, 2))));
374 return build1 (NOP_EXPR
, type
, expr
);
377 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
380 return convert (type
,
381 fold (build1 (REALPART_EXPR
,
382 TREE_TYPE (TREE_TYPE (expr
)), expr
)));
385 error ("aggregate value used where an integer was expected");
386 return convert (type
, integer_zero_node
);
390 /* Convert EXPR to the complex type TYPE in the usual ways. */
393 convert_to_complex (type
, expr
)
396 tree subtype
= TREE_TYPE (type
);
398 switch (TREE_CODE (TREE_TYPE (expr
)))
405 return build (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
406 convert (subtype
, integer_zero_node
));
410 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
412 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
414 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
415 return fold (build (COMPLEX_EXPR
,
417 convert (subtype
, TREE_OPERAND (expr
, 0)),
418 convert (subtype
, TREE_OPERAND (expr
, 1))));
421 expr
= save_expr (expr
);
423 fold (build (COMPLEX_EXPR
,
424 type
, convert (subtype
,
425 fold (build1 (REALPART_EXPR
,
426 TREE_TYPE (TREE_TYPE (expr
)),
429 fold (build1 (IMAGPART_EXPR
,
430 TREE_TYPE (TREE_TYPE (expr
)),
437 error ("pointer value used where a complex was expected");
438 return convert_to_complex (type
, integer_zero_node
);
441 error ("aggregate value used where a complex was expected");
442 return convert_to_complex (type
, integer_zero_node
);
This page took 0.267531 seconds and 5 git commands to generate.