1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 1988, 1991, 1992 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 if (TYPE_MODE (TREE_TYPE (expr)) != TYPE_MODE (type))
60 /* There is supposed to be some integral type
61 that is the same width as a pointer. */
63 return convert_to_pointer (type, expr);
66 error ("cannot convert to a pointer type");
68 return null_pointer_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 register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
82 if (form == REAL_TYPE)
83 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
86 if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
87 return build1 (FLOAT_EXPR, type, expr);
89 if (form == POINTER_TYPE)
90 error ("pointer value used where a floating point value was expected");
92 error ("aggregate value used where a float was expected");
95 register tree tem = make_node (REAL_CST);
96 TREE_TYPE (tem) = type;
97 TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0");
102 /* Convert EXPR to some integer (or enum) type TYPE.
104 EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
105 in other cases error is called.
107 The result of this is always supposed to be a newly created tree node
108 not in use in any existing structure. */
111 convert_to_integer (type, expr)
114 register tree intype = TREE_TYPE (expr);
115 register enum tree_code form = TREE_CODE (intype);
117 if (form == POINTER_TYPE)
119 if (integer_zerop (expr))
120 expr = integer_zero_node;
122 expr = fold (build1 (CONVERT_EXPR,
123 type_for_size (POINTER_SIZE, 0), expr));
124 intype = TREE_TYPE (expr);
125 form = TREE_CODE (intype);
130 if (form == INTEGER_TYPE || form == ENUMERAL_TYPE
131 || form == BOOLEAN_TYPE || form == CHAR_TYPE)
133 register unsigned outprec = TYPE_PRECISION (type);
134 register unsigned inprec = TYPE_PRECISION (intype);
135 register enum tree_code ex_form = TREE_CODE (expr);
137 /* If we are widening the type, put in an explicit conversion.
138 Similarly if we are not changing the width. However, if this is
139 a logical operation that just returns 0 or 1, we can change the
140 type of the expression (see below). */
142 if (TREE_CODE_CLASS (ex_form) == '<'
143 || ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
144 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
145 || ex_form == TRUTH_XOR_EXPR || ex_form == TRUTH_NOT_EXPR)
147 TREE_TYPE (expr) = type;
150 else if (outprec >= inprec)
151 return build1 (NOP_EXPR, type, expr);
153 /* Here detect when we can distribute the truncation down past some arithmetic.
154 For example, if adding two longs and converting to an int,
155 we can equally well convert both to ints and then add.
156 For the operations handled here, such truncation distribution
158 It is desirable in these cases:
159 1) when truncating down to full-word from a larger size
160 2) when truncating takes no work.
161 3) when at least one operand of the arithmetic has been extended
162 (as by C's default conversions). In this case we need two conversions
163 if we do the arithmetic as already requested, so we might as well
164 truncate both and then combine. Perhaps that way we need only one.
166 Note that in general we cannot do the arithmetic in a type
167 shorter than the desired result of conversion, even if the operands
168 are both extended from a shorter type, because they might overflow
169 if combined in that type. The exceptions to this--the times when
170 two narrow values can be combined in their narrow type even to
171 make a wider result--are handled by "shorten" in build_binary_op. */
176 if (TREE_UNSIGNED (type))
178 if (TREE_INT_CST_LOW (expr) >> outprec)
179 warning ("integer constant truncated");
183 /* if the sign bit of the low-order part isn't replicated
184 through the entire high part, we have overflow */
185 int sign = TREE_INT_CST_LOW (expr) & (1 << (outprec - 1));
186 if (!sign) /* lower part positive */
188 if (TREE_INT_CST_LOW (expr) >> outprec)
189 warning ("integer constant truncated");
193 if ((TREE_INT_CST_LOW (expr) >> outprec) + 1)
194 warning ("integer constant truncated");
200 /* We can pass truncation down through right shifting
201 when the shift count is a nonpositive constant. */
202 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
203 && tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_one_node))
208 /* We can pass truncation down through left shifting
209 when the shift count is a nonnegative constant. */
210 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
211 && ! tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_zero_node)
212 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
214 /* If shift count is less than the width of the truncated type,
216 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
217 /* 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 warning. */
224 return convert_to_integer (type, integer_zero_node);
232 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
233 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
235 /* Don't distribute unless the output precision is at least as big
236 as the actual inputs. Otherwise, the comparison of the
237 truncated values will be wrong. */
238 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
239 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
240 /* If signedness of arg0 and arg1 don't match,
241 we can't necessarily find a type to compare them in. */
242 && (TREE_UNSIGNED (TREE_TYPE (arg0))
243 == TREE_UNSIGNED (TREE_TYPE (arg1))))
256 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
257 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
259 if (outprec >= BITS_PER_WORD
260 || TRULY_NOOP_TRUNCATION (outprec, inprec)
261 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
262 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
264 /* Do the arithmetic in type TYPEX,
265 then convert result to TYPE. */
266 register tree typex = type;
268 /* Can't do arithmetic in enumeral types
269 so use an integer type that will hold the values. */
270 if (TREE_CODE (typex) == ENUMERAL_TYPE)
271 typex = type_for_size (TYPE_PRECISION (typex),
272 TREE_UNSIGNED (typex));
274 /* But now perhaps TYPEX is as wide as INPREC.
275 In that case, do nothing special here.
276 (Otherwise would recurse infinitely in convert. */
277 if (TYPE_PRECISION (typex) != inprec)
279 /* Don't do unsigned arithmetic where signed was wanted,
281 Exception: if either of the original operands were
282 unsigned then can safely do the work as unsigned.
283 And we may need to do it as unsigned
284 if we truncate to the original size. */
285 typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
286 || TREE_UNSIGNED (TREE_TYPE (arg0))
287 || TREE_UNSIGNED (TREE_TYPE (arg1)))
288 ? unsigned_type (typex) : signed_type (typex));
289 return convert (type,
290 build_binary_op (ex_form,
291 convert (typex, arg0),
292 convert (typex, arg1),
303 register tree typex = type;
305 /* Can't do arithmetic in enumeral types
306 so use an integer type that will hold the values. */
307 if (TREE_CODE (typex) == ENUMERAL_TYPE)
308 typex = type_for_size (TYPE_PRECISION (typex),
309 TREE_UNSIGNED (typex));
311 /* But now perhaps TYPEX is as wide as INPREC.
312 In that case, do nothing special here.
313 (Otherwise would recurse infinitely in convert. */
314 if (TYPE_PRECISION (typex) != inprec)
316 /* Don't do unsigned arithmetic where signed was wanted,
318 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
319 ? unsigned_type (typex) : signed_type (typex));
320 return convert (type,
321 build_unary_op (ex_form,
322 convert (typex, TREE_OPERAND (expr, 0)),
328 /* If truncating after truncating, might as well do all at once.
329 If truncating after extending, we may get rid of wasted work. */
330 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
333 /* Can treat the two alternative values like the operands
334 of an arithmetic expression. */
336 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
337 tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
339 if (outprec >= BITS_PER_WORD
340 || TRULY_NOOP_TRUNCATION (outprec, inprec)
341 || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
342 || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
344 /* Do the arithmetic in type TYPEX,
345 then convert result to TYPE. */
346 register tree typex = type;
348 /* Can't do arithmetic in enumeral types
349 so use an integer type that will hold the values. */
350 if (TREE_CODE (typex) == ENUMERAL_TYPE)
351 typex = type_for_size (TYPE_PRECISION (typex),
352 TREE_UNSIGNED (typex));
354 /* But now perhaps TYPEX is as wide as INPREC.
355 In that case, do nothing special here.
356 (Otherwise would recurse infinitely in convert. */
357 if (TYPE_PRECISION (typex) != inprec)
359 /* Don't do unsigned arithmetic where signed was wanted,
361 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
362 ? unsigned_type (typex) : signed_type (typex));
363 return convert (type,
364 fold (build (COND_EXPR, typex,
365 TREE_OPERAND (expr, 0),
366 convert (typex, arg1),
367 convert (typex, arg2))));
370 /* It is sometimes worthwhile
371 to push the narrowing down through the conditional. */
372 return fold (build (COND_EXPR, type,
373 TREE_OPERAND (expr, 0),
374 convert (type, TREE_OPERAND (expr, 1)),
375 convert (type, TREE_OPERAND (expr, 2))));
381 return build1 (NOP_EXPR, type, expr);
384 if (form == REAL_TYPE)
385 return build1 (FIX_TRUNC_EXPR, type, expr);
387 error ("aggregate value used where an integer was expected");
390 register tree tem = build_int_2 (0, 0);
391 TREE_TYPE (tem) = type;