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, 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. */
30 /* Convert EXPR to some pointer or reference type TYPE.
32 EXPR must be pointer, reference, integer, enumeral, or literal zero;
33 in other cases error is called. */
36 convert_to_pointer (type, expr)
39 register tree intype = TREE_TYPE (expr);
40 register enum tree_code form = TREE_CODE (intype);
42 if (integer_zerop (expr))
44 expr = build_int_2 (0, 0);
45 TREE_TYPE (expr) = type;
49 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
50 return build1 (NOP_EXPR, type, expr);
53 if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
55 if (type_precision (intype) == POINTER_SIZE)
56 return build1 (CONVERT_EXPR, type, expr);
57 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
58 /* Modes may be different but sizes should be the same. */
59 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
60 != GET_MODE_SIZE (TYPE_MODE (type)))
61 /* There is supposed to be some integral type
62 that is the same width as a pointer. */
64 return convert_to_pointer (type, expr);
67 error ("cannot convert to a pointer type");
69 expr = build_int_2 (0, 0);
70 TREE_TYPE (expr) = type;
74 /* Convert EXPR to some floating-point type TYPE.
76 EXPR must be float, integer, or enumeral;
77 in other cases error is called. */
80 convert_to_real (type, expr)
83 register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
85 if (form == REAL_TYPE)
86 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
89 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
90 return build1 (FLOAT_EXPR, type, expr);
92 if (form == COMPLEX_TYPE)
93 return convert (type, fold (build1 (REALPART_EXPR,
94 TREE_TYPE (TREE_TYPE (expr)), expr)));
96 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
97 error ("pointer value used where a floating point value was expected");
99 error ("aggregate value used where a float was expected");
102 register tree tem = make_node (REAL_CST);
103 TREE_TYPE (tem) = type;
104 TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0", TYPE_MODE (type));
109 /* Convert EXPR to some integer (or enum) type TYPE.
111 EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
112 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 register tree intype = TREE_TYPE (expr);
122 register enum tree_code form = TREE_CODE (intype);
124 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
126 if (integer_zerop (expr))
127 expr = integer_zero_node;
129 expr = fold (build1 (CONVERT_EXPR,
130 type_for_size (POINTER_SIZE, 0), expr));
131 intype = TREE_TYPE (expr);
132 form = TREE_CODE (intype);
137 if (form == INTEGER_TYPE || form == ENUMERAL_TYPE
138 || form == BOOLEAN_TYPE || form == CHAR_TYPE)
140 register unsigned outprec = TYPE_PRECISION (type);
141 register unsigned inprec = TYPE_PRECISION (intype);
142 register enum tree_code ex_form = TREE_CODE (expr);
144 /* If we are widening the type, put in an explicit conversion.
145 Similarly if we are not changing the width. However, if this is
146 a logical operation that just returns 0 or 1, we can change the
147 type of the expression. For logical operations, we must
148 also change the types of the operands to maintain type
151 if (TREE_CODE_CLASS (ex_form) == '<')
153 TREE_TYPE (expr) = type;
156 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
157 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
158 || ex_form == TRUTH_XOR_EXPR)
160 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
161 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
162 TREE_TYPE (expr) = type;
165 else if (ex_form == TRUTH_NOT_EXPR)
167 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
168 TREE_TYPE (expr) = type;
171 else if (outprec >= inprec)
172 return build1 (NOP_EXPR, type, expr);
174 /* Here detect when we can distribute the truncation down past some
175 arithmetic. For example, if adding two longs and converting to an
176 int, we can equally well convert both to ints and then add.
177 For the operations handled here, such truncation distribution
179 It is desirable in these cases:
180 1) when truncating down to full-word from a larger size
181 2) when truncating takes no work.
182 3) when at least one operand of the arithmetic has been extended
183 (as by C's default conversions). In this case we need two conversions
184 if we do the arithmetic as already requested, so we might as well
185 truncate both and then combine. Perhaps that way we need only one.
187 Note that in general we cannot do the arithmetic in a type
188 shorter than the desired result of conversion, even if the operands
189 are both extended from a shorter type, because they might overflow
190 if combined in that type. The exceptions to this--the times when
191 two narrow values can be combined in their narrow type even to
192 make a wider result--are handled by "shorten" in build_binary_op. */
197 /* We can pass truncation down through right shifting
198 when the shift count is a nonpositive constant. */
199 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
200 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
201 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
207 /* We can pass truncation down through left shifting
208 when the shift count is a nonnegative constant. */
209 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
210 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
211 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
213 /* If shift count is less than the width of the truncated type,
215 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
216 /* In this case, shifting is like multiplication. */
220 /* If it is >= that width, result is zero.
221 Handling this with trunc1 would give the wrong result:
222 (int) ((long long) a << 32) is well defined (as 0)
223 but (int) a << 32 is undefined and would get a
226 tree t = convert_to_integer (type, integer_zero_node);
228 /* If the original expression had side-effects, we must
230 if (TREE_SIDE_EFFECTS (expr))
231 return build (COMPOUND_EXPR, type, expr, t);
242 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
243 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
245 /* Don't distribute unless the output precision is at least as big
246 as the actual inputs. Otherwise, the comparison of the
247 truncated values will be wrong. */
248 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
249 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
250 /* If signedness of arg0 and arg1 don't match,
251 we can't necessarily find a type to compare them in. */
252 && (TREE_UNSIGNED (TREE_TYPE (arg0))
253 == TREE_UNSIGNED (TREE_TYPE (arg1))))
266 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
267 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
269 if (outprec >= BITS_PER_WORD
270 || TRULY_NOOP_TRUNCATION (outprec, inprec)
271 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
272 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
274 /* Do the arithmetic in type TYPEX,
275 then convert result to TYPE. */
276 register tree typex = type;
278 /* Can't do arithmetic in enumeral types
279 so use an integer type that will hold the values. */
280 if (TREE_CODE (typex) == ENUMERAL_TYPE)
281 typex = type_for_size (TYPE_PRECISION (typex),
282 TREE_UNSIGNED (typex));
284 /* But now perhaps TYPEX is as wide as INPREC.
285 In that case, do nothing special here.
286 (Otherwise would recurse infinitely in convert. */
287 if (TYPE_PRECISION (typex) != inprec)
289 /* Don't do unsigned arithmetic where signed was wanted,
291 Exception: if either of the original operands were
292 unsigned then can safely do the work as unsigned.
293 And we may need to do it as unsigned
294 if we truncate to the original size. */
295 typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
296 || TREE_UNSIGNED (TREE_TYPE (arg0))
297 || TREE_UNSIGNED (TREE_TYPE (arg1)))
298 ? unsigned_type (typex) : signed_type (typex));
299 return convert (type,
300 fold (build (ex_form, typex,
301 convert (typex, arg0),
302 convert (typex, arg1),
311 /* This is not correct for ABS_EXPR,
312 since we must test the sign before truncation. */
314 register tree typex = type;
316 /* Can't do arithmetic in enumeral types
317 so use an integer type that will hold the values. */
318 if (TREE_CODE (typex) == ENUMERAL_TYPE)
319 typex = type_for_size (TYPE_PRECISION (typex),
320 TREE_UNSIGNED (typex));
322 /* But now perhaps TYPEX is as wide as INPREC.
323 In that case, do nothing special here.
324 (Otherwise would recurse infinitely in convert. */
325 if (TYPE_PRECISION (typex) != inprec)
327 /* Don't do unsigned arithmetic where signed was wanted,
329 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
330 ? unsigned_type (typex) : signed_type (typex));
331 return convert (type,
332 fold (build1 (ex_form, typex,
334 TREE_OPERAND (expr, 0)))));
339 /* If truncating after truncating, might as well do all at once.
340 If truncating after extending, we may get rid of wasted work. */
341 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
344 /* Can treat the two alternative values like the operands
345 of an arithmetic expression. */
347 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
348 tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
350 if (outprec >= BITS_PER_WORD
351 || TRULY_NOOP_TRUNCATION (outprec, inprec)
352 || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
353 || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
355 /* Do the arithmetic in type TYPEX,
356 then convert result to TYPE. */
357 register tree typex = type;
359 /* Can't do arithmetic in enumeral types
360 so use an integer type that will hold the values. */
361 if (TREE_CODE (typex) == ENUMERAL_TYPE)
362 typex = type_for_size (TYPE_PRECISION (typex),
363 TREE_UNSIGNED (typex));
365 /* But now perhaps TYPEX is as wide as INPREC.
366 In that case, do nothing special here.
367 (Otherwise would recurse infinitely in convert. */
368 if (TYPE_PRECISION (typex) != inprec)
370 /* Don't do unsigned arithmetic where signed was wanted,
372 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
373 ? unsigned_type (typex) : signed_type (typex));
374 return convert (type,
375 fold (build (COND_EXPR, typex,
376 TREE_OPERAND (expr, 0),
377 convert (typex, arg1),
378 convert (typex, arg2))));
381 /* It is sometimes worthwhile
382 to push the narrowing down through the conditional. */
383 return fold (build (COND_EXPR, type,
384 TREE_OPERAND (expr, 0),
385 convert (type, TREE_OPERAND (expr, 1)),
386 convert (type, TREE_OPERAND (expr, 2))));
392 return build1 (NOP_EXPR, type, expr);
395 if (form == REAL_TYPE)
396 return build1 (FIX_TRUNC_EXPR, type, expr);
398 if (form == COMPLEX_TYPE)
399 return convert (type, fold (build1 (REALPART_EXPR,
400 TREE_TYPE (TREE_TYPE (expr)), expr)));
402 error ("aggregate value used where an integer was expected");
405 register tree tem = build_int_2 (0, 0);
406 TREE_TYPE (tem) = type;
411 /* Convert EXPR to the complex type TYPE in the usual ways. */
414 convert_to_complex (type, expr)
417 register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
418 tree subtype = TREE_TYPE (type);
420 if (form == REAL_TYPE || form == INTEGER_TYPE || form == ENUMERAL_TYPE)
422 expr = convert (subtype, expr);
423 return build (COMPLEX_EXPR, type, expr,
424 convert (subtype, integer_zero_node));
427 if (form == COMPLEX_TYPE)
429 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
430 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
432 else if (TREE_CODE (expr) == COMPLEX_EXPR)
433 return fold (build (COMPLEX_EXPR,
435 convert (subtype, TREE_OPERAND (expr, 0)),
436 convert (subtype, TREE_OPERAND (expr, 1))));
439 expr = save_expr (expr);
440 return fold (build (COMPLEX_EXPR,
443 fold (build1 (REALPART_EXPR,
444 TREE_TYPE (TREE_TYPE (expr)),
447 fold (build1 (IMAGPART_EXPR,
448 TREE_TYPE (TREE_TYPE (expr)),
453 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
454 error ("pointer value used where a complex was expected");
456 error ("aggregate value used where a complex was expected");
458 return build (COMPLEX_EXPR, type,
459 convert (subtype, integer_zero_node),
460 convert (subtype, integer_zero_node));