Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / convert.c
blob195c2dd78ed05bfa1a11b547896f40dec48b63be
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)
9 any later version.
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. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "convert.h"
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. */
36 tree
37 convert_to_pointer (type, expr)
38 tree type, expr;
40 if (integer_zerop (expr))
42 expr = build_int_2 (0, 0);
43 TREE_TYPE (expr) = type;
44 return expr;
47 switch (TREE_CODE (TREE_TYPE (expr)))
49 case POINTER_TYPE:
50 case REFERENCE_TYPE:
51 return build1 (NOP_EXPR, type, expr);
53 case INTEGER_TYPE:
54 case ENUMERAL_TYPE:
55 case BOOLEAN_TYPE:
56 case CHAR_TYPE:
57 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
58 return build1 (CONVERT_EXPR, type, expr);
60 return
61 convert_to_pointer (type,
62 convert (type_for_size (POINTER_SIZE, 0), expr));
64 default:
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. */
75 tree
76 convert_to_real (type, expr)
77 tree type, expr;
79 switch (TREE_CODE (TREE_TYPE (expr)))
81 case REAL_TYPE:
82 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
83 type, expr);
85 case INTEGER_TYPE:
86 case ENUMERAL_TYPE:
87 case BOOLEAN_TYPE:
88 case CHAR_TYPE:
89 return build1 (FLOAT_EXPR, type, expr);
91 case COMPLEX_TYPE:
92 return convert (type,
93 fold (build1 (REALPART_EXPR,
94 TREE_TYPE (TREE_TYPE (expr)), expr)));
96 case POINTER_TYPE:
97 case REFERENCE_TYPE:
98 error ("pointer value used where a floating point value was expected");
99 return convert_to_real (type, integer_zero_node);
101 default:
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. */
115 tree
116 convert_to_integer (type, expr)
117 tree 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))
126 case POINTER_TYPE:
127 case REFERENCE_TYPE:
128 if (integer_zerop (expr))
129 expr = integer_zero_node;
130 else
131 expr = fold (build1 (CONVERT_EXPR,
132 type_for_size (POINTER_SIZE, 0), expr));
134 return convert_to_integer (type, expr);
136 case INTEGER_TYPE:
137 case ENUMERAL_TYPE:
138 case BOOLEAN_TYPE:
139 case CHAR_TYPE:
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
143 correctness. */
145 if (TREE_CODE_CLASS (ex_form) == '<')
147 TREE_TYPE (expr) = type;
148 return expr;
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;
158 return expr;
161 else if (ex_form == TRUTH_NOT_EXPR)
163 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
164 TREE_TYPE (expr) = type;
165 return expr;
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
178 to TYPE. */
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)),
184 expr));
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
190 is always safe.
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. */
206 switch (ex_form)
208 case RSHIFT_EXPR:
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)),
214 integer_one_node)))
215 goto trunc1;
216 break;
218 case LSHIFT_EXPR:
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,
226 really shift. */
227 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
228 /* In this case, shifting is like multiplication. */
229 goto trunc1;
230 else
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
236 warning. */
238 tree t = convert_to_integer (type, integer_zero_node);
240 /* If the original expression had side-effects, we must
241 preserve it. */
242 if (TREE_SIDE_EFFECTS (expr))
243 return build (COMPOUND_EXPR, type, expr, t);
244 else
245 return t;
248 break;
250 case MAX_EXPR:
251 case MIN_EXPR:
252 case MULT_EXPR:
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))))
266 goto trunc1;
267 break;
270 case PLUS_EXPR:
271 case MINUS_EXPR:
272 case BIT_AND_EXPR:
273 case BIT_IOR_EXPR:
274 case BIT_XOR_EXPR:
275 case BIT_ANDTC_EXPR:
276 trunc1:
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,
302 or vice versa.
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),
315 0)));
319 break;
321 case NEGATE_EXPR:
322 case BIT_NOT_EXPR:
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,
340 or vice versa. */
341 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
342 ? unsigned_type (typex) : signed_type (typex));
343 return convert (type,
344 fold (build1 (ex_form, typex,
345 convert (typex,
346 TREE_OPERAND (expr, 0)))));
350 case NOP_EXPR:
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));
355 case COND_EXPR:
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))));
362 default:
363 break;
366 return build1 (NOP_EXPR, type, expr);
368 case REAL_TYPE:
369 return build1 (FIX_TRUNC_EXPR, type, expr);
371 case COMPLEX_TYPE:
372 return convert (type,
373 fold (build1 (REALPART_EXPR,
374 TREE_TYPE (TREE_TYPE (expr)), expr)));
376 default:
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. */
384 tree
385 convert_to_complex (type, expr)
386 tree type, expr;
388 tree subtype = TREE_TYPE (type);
390 switch (TREE_CODE (TREE_TYPE (expr)))
392 case REAL_TYPE:
393 case INTEGER_TYPE:
394 case ENUMERAL_TYPE:
395 case BOOLEAN_TYPE:
396 case CHAR_TYPE:
397 return build (COMPLEX_EXPR, type, convert (subtype, expr),
398 convert (subtype, integer_zero_node));
400 case COMPLEX_TYPE:
402 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
404 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
405 return expr;
406 else if (TREE_CODE (expr) == COMPLEX_EXPR)
407 return fold (build (COMPLEX_EXPR,
408 type,
409 convert (subtype, TREE_OPERAND (expr, 0)),
410 convert (subtype, TREE_OPERAND (expr, 1))));
411 else
413 expr = save_expr (expr);
414 return
415 fold (build (COMPLEX_EXPR,
416 type, convert (subtype,
417 fold (build1 (REALPART_EXPR,
418 TREE_TYPE (TREE_TYPE (expr)),
419 expr))),
420 convert (subtype,
421 fold (build1 (IMAGPART_EXPR,
422 TREE_TYPE (TREE_TYPE (expr)),
423 expr)))));
427 case POINTER_TYPE:
428 case REFERENCE_TYPE:
429 error ("pointer value used where a complex was expected");
430 return convert_to_complex (type, integer_zero_node);
432 default:
433 error ("aggregate value used where a complex was expected");
434 return convert_to_complex (type, integer_zero_node);