(thumb_unexpanded_epilogue): Don't generate epilogue for naked functions.
[official-gcc.git] / gcc / convert.c
blobe440e35f57577a9f0bedbf1b124f9f8a1aabbd97
1 /* Utility routines for data type conversion for GNU C.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997,
3 1998 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "toplev.h"
32 #include "langhooks.h"
34 /* Convert EXPR to some pointer or reference type TYPE.
36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
37 in other cases error is called. */
39 tree
40 convert_to_pointer (type, expr)
41 tree type, expr;
43 if (integer_zerop (expr))
45 expr = build_int_2 (0, 0);
46 TREE_TYPE (expr) = type;
47 return expr;
50 switch (TREE_CODE (TREE_TYPE (expr)))
52 case POINTER_TYPE:
53 case REFERENCE_TYPE:
54 return build1 (NOP_EXPR, type, expr);
56 case INTEGER_TYPE:
57 case ENUMERAL_TYPE:
58 case BOOLEAN_TYPE:
59 case CHAR_TYPE:
60 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
61 return build1 (CONVERT_EXPR, type, expr);
63 return
64 convert_to_pointer (type,
65 convert ((*lang_hooks.types.type_for_size)
66 (POINTER_SIZE, 0), expr));
68 default:
69 error ("cannot convert to a pointer type");
70 return convert_to_pointer (type, integer_zero_node);
74 /* Convert EXPR to some floating-point type TYPE.
76 EXPR must be float, integer, or enumeral;
77 in other cases error is called. */
79 tree
80 convert_to_real (type, expr)
81 tree type, expr;
83 switch (TREE_CODE (TREE_TYPE (expr)))
85 case REAL_TYPE:
86 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
87 type, expr);
89 case INTEGER_TYPE:
90 case ENUMERAL_TYPE:
91 case BOOLEAN_TYPE:
92 case CHAR_TYPE:
93 return build1 (FLOAT_EXPR, type, expr);
95 case COMPLEX_TYPE:
96 return convert (type,
97 fold (build1 (REALPART_EXPR,
98 TREE_TYPE (TREE_TYPE (expr)), expr)));
100 case POINTER_TYPE:
101 case REFERENCE_TYPE:
102 error ("pointer value used where a floating point value was expected");
103 return convert_to_real (type, integer_zero_node);
105 default:
106 error ("aggregate value used where a float was expected");
107 return convert_to_real (type, integer_zero_node);
111 /* Convert EXPR to some integer (or enum) type TYPE.
113 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
114 vector; in other cases error is called.
116 The result of this is always supposed to be a newly created tree node
117 not in use in any existing structure. */
119 tree
120 convert_to_integer (type, expr)
121 tree type, expr;
123 enum tree_code ex_form = TREE_CODE (expr);
124 tree intype = TREE_TYPE (expr);
125 unsigned int inprec = TYPE_PRECISION (intype);
126 unsigned int outprec = TYPE_PRECISION (type);
128 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
129 be. Consider `enum E = { a, b = (enum E) 3 };'. */
130 if (!COMPLETE_TYPE_P (type))
132 error ("conversion to incomplete type");
133 return error_mark_node;
136 switch (TREE_CODE (intype))
138 case POINTER_TYPE:
139 case REFERENCE_TYPE:
140 if (integer_zerop (expr))
141 expr = integer_zero_node;
142 else
143 expr = fold (build1 (CONVERT_EXPR, (*lang_hooks.types.type_for_size)
144 (POINTER_SIZE, 0), expr));
146 return convert_to_integer (type, expr);
148 case INTEGER_TYPE:
149 case ENUMERAL_TYPE:
150 case BOOLEAN_TYPE:
151 case CHAR_TYPE:
152 /* If this is a logical operation, which just returns 0 or 1, we can
153 change the type of the expression. For some logical operations,
154 we must also change the types of the operands to maintain type
155 correctness. */
157 if (TREE_CODE_CLASS (ex_form) == '<')
159 TREE_TYPE (expr) = type;
160 return expr;
163 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
164 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
165 || ex_form == TRUTH_XOR_EXPR)
167 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
168 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
169 TREE_TYPE (expr) = type;
170 return expr;
173 else if (ex_form == TRUTH_NOT_EXPR)
175 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
176 TREE_TYPE (expr) = type;
177 return expr;
180 /* If we are widening the type, put in an explicit conversion.
181 Similarly if we are not changing the width. After this, we know
182 we are truncating EXPR. */
184 else if (outprec >= inprec)
185 return build1 (NOP_EXPR, type, expr);
187 /* If TYPE is an enumeral type or a type with a precision less
188 than the number of bits in its mode, do the conversion to the
189 type corresponding to its mode, then do a nop conversion
190 to TYPE. */
191 else if (TREE_CODE (type) == ENUMERAL_TYPE
192 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
193 return build1 (NOP_EXPR, type,
194 convert ((*lang_hooks.types.type_for_mode)
195 (TYPE_MODE (type), TREE_UNSIGNED (type)),
196 expr));
198 /* Here detect when we can distribute the truncation down past some
199 arithmetic. For example, if adding two longs and converting to an
200 int, we can equally well convert both to ints and then add.
201 For the operations handled here, such truncation distribution
202 is always safe.
203 It is desirable in these cases:
204 1) when truncating down to full-word from a larger size
205 2) when truncating takes no work.
206 3) when at least one operand of the arithmetic has been extended
207 (as by C's default conversions). In this case we need two conversions
208 if we do the arithmetic as already requested, so we might as well
209 truncate both and then combine. Perhaps that way we need only one.
211 Note that in general we cannot do the arithmetic in a type
212 shorter than the desired result of conversion, even if the operands
213 are both extended from a shorter type, because they might overflow
214 if combined in that type. The exceptions to this--the times when
215 two narrow values can be combined in their narrow type even to
216 make a wider result--are handled by "shorten" in build_binary_op. */
218 switch (ex_form)
220 case RSHIFT_EXPR:
221 /* We can pass truncation down through right shifting
222 when the shift count is a nonpositive constant. */
223 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
224 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
225 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
226 integer_one_node)))
227 goto trunc1;
228 break;
230 case LSHIFT_EXPR:
231 /* We can pass truncation down through left shifting
232 when the shift count is a nonnegative constant and
233 the target type is unsigned. */
234 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
235 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
236 && TREE_UNSIGNED (type)
237 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
239 /* If shift count is less than the width of the truncated type,
240 really shift. */
241 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
242 /* In this case, shifting is like multiplication. */
243 goto trunc1;
244 else
246 /* If it is >= that width, result is zero.
247 Handling this with trunc1 would give the wrong result:
248 (int) ((long long) a << 32) is well defined (as 0)
249 but (int) a << 32 is undefined and would get a
250 warning. */
252 tree t = convert_to_integer (type, integer_zero_node);
254 /* If the original expression had side-effects, we must
255 preserve it. */
256 if (TREE_SIDE_EFFECTS (expr))
257 return build (COMPOUND_EXPR, type, expr, t);
258 else
259 return t;
262 break;
264 case MAX_EXPR:
265 case MIN_EXPR:
266 case MULT_EXPR:
268 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
269 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
271 /* Don't distribute unless the output precision is at least as big
272 as the actual inputs. Otherwise, the comparison of the
273 truncated values will be wrong. */
274 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
275 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
276 /* If signedness of arg0 and arg1 don't match,
277 we can't necessarily find a type to compare them in. */
278 && (TREE_UNSIGNED (TREE_TYPE (arg0))
279 == TREE_UNSIGNED (TREE_TYPE (arg1))))
280 goto trunc1;
281 break;
284 case PLUS_EXPR:
285 case MINUS_EXPR:
286 case BIT_AND_EXPR:
287 case BIT_IOR_EXPR:
288 case BIT_XOR_EXPR:
289 case BIT_ANDTC_EXPR:
290 trunc1:
292 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
293 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
295 if (outprec >= BITS_PER_WORD
296 || TRULY_NOOP_TRUNCATION (outprec, inprec)
297 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
298 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
300 /* Do the arithmetic in type TYPEX,
301 then convert result to TYPE. */
302 tree typex = type;
304 /* Can't do arithmetic in enumeral types
305 so use an integer type that will hold the values. */
306 if (TREE_CODE (typex) == ENUMERAL_TYPE)
307 typex = (*lang_hooks.types.type_for_size)
308 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
310 /* But now perhaps TYPEX is as wide as INPREC.
311 In that case, do nothing special here.
312 (Otherwise would recurse infinitely in convert. */
313 if (TYPE_PRECISION (typex) != inprec)
315 /* Don't do unsigned arithmetic where signed was wanted,
316 or vice versa.
317 Exception: if both of the original operands were
318 unsigned then we can safely do the work as unsigned.
319 Exception: shift operations take their type solely
320 from the first argument.
321 Exception: the LSHIFT_EXPR case above requires that
322 we perform this operation unsigned lest we produce
323 signed-overflow undefinedness.
324 And we may need to do it as unsigned
325 if we truncate to the original size. */
326 if (TREE_UNSIGNED (TREE_TYPE (expr))
327 || (TREE_UNSIGNED (TREE_TYPE (arg0))
328 && (TREE_UNSIGNED (TREE_TYPE (arg1))
329 || ex_form == LSHIFT_EXPR
330 || ex_form == RSHIFT_EXPR
331 || ex_form == LROTATE_EXPR
332 || ex_form == RROTATE_EXPR))
333 || ex_form == LSHIFT_EXPR)
334 typex = (*lang_hooks.types.unsigned_type) (typex);
335 else
336 typex = (*lang_hooks.types.signed_type) (typex);
337 return convert (type,
338 fold (build (ex_form, typex,
339 convert (typex, arg0),
340 convert (typex, arg1),
341 0)));
345 break;
347 case NEGATE_EXPR:
348 case BIT_NOT_EXPR:
349 /* This is not correct for ABS_EXPR,
350 since we must test the sign before truncation. */
352 tree typex = type;
354 /* Can't do arithmetic in enumeral types
355 so use an integer type that will hold the values. */
356 if (TREE_CODE (typex) == ENUMERAL_TYPE)
357 typex = (*lang_hooks.types.type_for_size)
358 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
360 /* But now perhaps TYPEX is as wide as INPREC.
361 In that case, do nothing special here.
362 (Otherwise would recurse infinitely in convert. */
363 if (TYPE_PRECISION (typex) != inprec)
365 /* Don't do unsigned arithmetic where signed was wanted,
366 or vice versa. */
367 if (TREE_UNSIGNED (TREE_TYPE (expr)))
368 typex = (*lang_hooks.types.unsigned_type) (typex);
369 else
370 typex = (*lang_hooks.types.signed_type) (typex);
371 return convert (type,
372 fold (build1 (ex_form, typex,
373 convert (typex,
374 TREE_OPERAND (expr, 0)))));
378 case NOP_EXPR:
379 /* Don't introduce a
380 "can't convert between vector values of different size" error. */
381 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
382 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
383 != GET_MODE_SIZE (TYPE_MODE (type))))
384 break;
385 /* If truncating after truncating, might as well do all at once.
386 If truncating after extending, we may get rid of wasted work. */
387 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
389 case COND_EXPR:
390 /* It is sometimes worthwhile to push the narrowing down through
391 the conditional and never loses. */
392 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
393 convert (type, TREE_OPERAND (expr, 1)),
394 convert (type, TREE_OPERAND (expr, 2))));
396 default:
397 break;
400 return build1 (NOP_EXPR, type, expr);
402 case REAL_TYPE:
403 return build1 (FIX_TRUNC_EXPR, type, expr);
405 case COMPLEX_TYPE:
406 return convert (type,
407 fold (build1 (REALPART_EXPR,
408 TREE_TYPE (TREE_TYPE (expr)), expr)));
410 case VECTOR_TYPE:
411 if (GET_MODE_SIZE (TYPE_MODE (type))
412 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
414 error ("can't convert between vector values of different size");
415 return error_mark_node;
417 return build1 (NOP_EXPR, type, expr);
419 default:
420 error ("aggregate value used where an integer was expected");
421 return convert (type, integer_zero_node);
425 /* Convert EXPR to the complex type TYPE in the usual ways. */
427 tree
428 convert_to_complex (type, expr)
429 tree type, expr;
431 tree subtype = TREE_TYPE (type);
433 switch (TREE_CODE (TREE_TYPE (expr)))
435 case REAL_TYPE:
436 case INTEGER_TYPE:
437 case ENUMERAL_TYPE:
438 case BOOLEAN_TYPE:
439 case CHAR_TYPE:
440 return build (COMPLEX_EXPR, type, convert (subtype, expr),
441 convert (subtype, integer_zero_node));
443 case COMPLEX_TYPE:
445 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
447 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
448 return expr;
449 else if (TREE_CODE (expr) == COMPLEX_EXPR)
450 return fold (build (COMPLEX_EXPR,
451 type,
452 convert (subtype, TREE_OPERAND (expr, 0)),
453 convert (subtype, TREE_OPERAND (expr, 1))));
454 else
456 expr = save_expr (expr);
457 return
458 fold (build (COMPLEX_EXPR,
459 type, convert (subtype,
460 fold (build1 (REALPART_EXPR,
461 TREE_TYPE (TREE_TYPE (expr)),
462 expr))),
463 convert (subtype,
464 fold (build1 (IMAGPART_EXPR,
465 TREE_TYPE (TREE_TYPE (expr)),
466 expr)))));
470 case POINTER_TYPE:
471 case REFERENCE_TYPE:
472 error ("pointer value used where a complex was expected");
473 return convert_to_complex (type, integer_zero_node);
475 default:
476 error ("aggregate value used where a complex was expected");
477 return convert_to_complex (type, integer_zero_node);
481 /* Convert EXPR to the vector type TYPE in the usual ways. */
483 tree
484 convert_to_vector (type, expr)
485 tree type, expr;
487 switch (TREE_CODE (TREE_TYPE (expr)))
489 case INTEGER_TYPE:
490 case VECTOR_TYPE:
491 if (GET_MODE_SIZE (TYPE_MODE (type))
492 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
494 error ("can't convert between vector values of different size");
495 return error_mark_node;
497 return build1 (NOP_EXPR, type, expr);
499 default:
500 error ("can't convert value to a vector");
501 return convert_to_vector (type, integer_zero_node);