]> gcc.gnu.org Git - gcc.git/blame - gcc/convert.c
configure.in (CROSS): Define NATIVE_CROSS.
[gcc.git] / gcc / convert.c
CommitLineData
76e616db 1/* Utility routines for data type conversion for GNU C.
3c71940f
JL
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997,
3 1998 Free Software Foundation, Inc.
76e616db 4
1322177d 5This file is part of GCC.
76e616db 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
76e616db 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
76e616db
BK
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
76e616db
BK
21
22
23/* These routines are somewhat language-independent utility function
0f41302f 24 intended to be called by the language-specific convert () functions. */
76e616db
BK
25
26#include "config.h"
c5c76735 27#include "system.h"
76e616db
BK
28#include "tree.h"
29#include "flags.h"
30#include "convert.h"
10f0ad3d 31#include "toplev.h"
b0c48229 32#include "langhooks.h"
76e616db 33
98c76e3c 34/* Convert EXPR to some pointer or reference type TYPE.
76e616db 35
98c76e3c 36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
0f41302f 37 in other cases error is called. */
76e616db
BK
38
39tree
40convert_to_pointer (type, expr)
41 tree type, expr;
42{
76e616db
BK
43 if (integer_zerop (expr))
44 {
76e616db
BK
45 expr = build_int_2 (0, 0);
46 TREE_TYPE (expr) = type;
47 return expr;
48 }
49
f5963e61 50 switch (TREE_CODE (TREE_TYPE (expr)))
76e616db 51 {
f5963e61
JL
52 case POINTER_TYPE:
53 case REFERENCE_TYPE:
54 return build1 (NOP_EXPR, type, expr);
55
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)
76e616db 61 return build1 (CONVERT_EXPR, type, expr);
76e616db 62
f5963e61
JL
63 return
64 convert_to_pointer (type,
b0c48229
NB
65 convert ((*lang_hooks.types.type_for_size)
66 (POINTER_SIZE, 0), expr));
76e616db 67
f5963e61
JL
68 default:
69 error ("cannot convert to a pointer type");
70 return convert_to_pointer (type, integer_zero_node);
71 }
76e616db
BK
72}
73
74/* Convert EXPR to some floating-point type TYPE.
75
76 EXPR must be float, integer, or enumeral;
0f41302f 77 in other cases error is called. */
76e616db
BK
78
79tree
80convert_to_real (type, expr)
81 tree type, expr;
82{
f5963e61
JL
83 switch (TREE_CODE (TREE_TYPE (expr)))
84 {
85 case REAL_TYPE:
86 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
87 type, expr);
88
89 case INTEGER_TYPE:
90 case ENUMERAL_TYPE:
91 case BOOLEAN_TYPE:
92 case CHAR_TYPE:
93 return build1 (FLOAT_EXPR, type, expr);
94
95 case COMPLEX_TYPE:
96 return convert (type,
97 fold (build1 (REALPART_EXPR,
98 TREE_TYPE (TREE_TYPE (expr)), expr)));
99
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);
104
105 default:
106 error ("aggregate value used where a float was expected");
107 return convert_to_real (type, integer_zero_node);
108 }
76e616db
BK
109}
110
111/* Convert EXPR to some integer (or enum) type TYPE.
112
0b4565c9
BS
113 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
114 vector; in other cases error is called.
76e616db
BK
115
116 The result of this is always supposed to be a newly created tree node
117 not in use in any existing structure. */
118
119tree
120convert_to_integer (type, expr)
121 tree type, expr;
122{
f5963e61
JL
123 enum tree_code ex_form = TREE_CODE (expr);
124 tree intype = TREE_TYPE (expr);
770ae6cc
RK
125 unsigned int inprec = TYPE_PRECISION (intype);
126 unsigned int outprec = TYPE_PRECISION (type);
76e616db 127
9c4cb3a3
MM
128 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
129 be. Consider `enum E = { a, b = (enum E) 3 };'. */
d0f062fb 130 if (!COMPLETE_TYPE_P (type))
9c4cb3a3
MM
131 {
132 error ("conversion to incomplete type");
133 return error_mark_node;
134 }
135
f5963e61 136 switch (TREE_CODE (intype))
76e616db 137 {
f5963e61
JL
138 case POINTER_TYPE:
139 case REFERENCE_TYPE:
76e616db
BK
140 if (integer_zerop (expr))
141 expr = integer_zero_node;
142 else
b0c48229
NB
143 expr = fold (build1 (CONVERT_EXPR, (*lang_hooks.types.type_for_size)
144 (POINTER_SIZE, 0), expr));
76e616db 145
f5963e61 146 return convert_to_integer (type, expr);
76e616db 147
f5963e61
JL
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
c9529354 155 correctness. */
76e616db 156
c9529354 157 if (TREE_CODE_CLASS (ex_form) == '<')
76e616db
BK
158 {
159 TREE_TYPE (expr) = type;
160 return expr;
161 }
f5963e61 162
c9529354
RK
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)
166 {
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;
171 }
f5963e61 172
c9529354
RK
173 else if (ex_form == TRUTH_NOT_EXPR)
174 {
175 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
176 TREE_TYPE (expr) = type;
177 return expr;
178 }
f5963e61
JL
179
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. */
183
76e616db
BK
184 else if (outprec >= inprec)
185 return build1 (NOP_EXPR, type, expr);
186
1c013b45
RK
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,
b0c48229
NB
194 convert ((*lang_hooks.types.type_for_mode)
195 (TYPE_MODE (type), TREE_UNSIGNED (type)),
1c013b45
RK
196 expr));
197
ab29fdfc
RK
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.
210
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. */
76e616db
BK
217
218 switch (ex_form)
219 {
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
ab29fdfc
RK
224 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
225 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
226 integer_one_node)))
76e616db
BK
227 goto trunc1;
228 break;
229
230 case LSHIFT_EXPR:
231 /* We can pass truncation down through left shifting
232 when the shift count is a nonnegative constant. */
233 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
ab29fdfc 234 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
76e616db
BK
235 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
236 {
237 /* If shift count is less than the width of the truncated type,
238 really shift. */
239 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
240 /* In this case, shifting is like multiplication. */
241 goto trunc1;
242 else
d9a9c5a7
RK
243 {
244 /* If it is >= that width, result is zero.
245 Handling this with trunc1 would give the wrong result:
246 (int) ((long long) a << 32) is well defined (as 0)
247 but (int) a << 32 is undefined and would get a
248 warning. */
249
250 tree t = convert_to_integer (type, integer_zero_node);
251
252 /* If the original expression had side-effects, we must
253 preserve it. */
254 if (TREE_SIDE_EFFECTS (expr))
255 return build (COMPOUND_EXPR, type, expr, t);
256 else
257 return t;
258 }
76e616db
BK
259 }
260 break;
261
262 case MAX_EXPR:
263 case MIN_EXPR:
264 case MULT_EXPR:
265 {
266 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
267 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
268
269 /* Don't distribute unless the output precision is at least as big
270 as the actual inputs. Otherwise, the comparison of the
271 truncated values will be wrong. */
272 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
273 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
274 /* If signedness of arg0 and arg1 don't match,
275 we can't necessarily find a type to compare them in. */
276 && (TREE_UNSIGNED (TREE_TYPE (arg0))
277 == TREE_UNSIGNED (TREE_TYPE (arg1))))
278 goto trunc1;
279 break;
280 }
281
282 case PLUS_EXPR:
283 case MINUS_EXPR:
284 case BIT_AND_EXPR:
285 case BIT_IOR_EXPR:
286 case BIT_XOR_EXPR:
287 case BIT_ANDTC_EXPR:
288 trunc1:
289 {
290 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
291 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
292
293 if (outprec >= BITS_PER_WORD
294 || TRULY_NOOP_TRUNCATION (outprec, inprec)
295 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
296 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
297 {
298 /* Do the arithmetic in type TYPEX,
299 then convert result to TYPE. */
b3694847 300 tree typex = type;
76e616db
BK
301
302 /* Can't do arithmetic in enumeral types
303 so use an integer type that will hold the values. */
304 if (TREE_CODE (typex) == ENUMERAL_TYPE)
b0c48229
NB
305 typex = (*lang_hooks.types.type_for_size)
306 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
76e616db
BK
307
308 /* But now perhaps TYPEX is as wide as INPREC.
309 In that case, do nothing special here.
310 (Otherwise would recurse infinitely in convert. */
311 if (TYPE_PRECISION (typex) != inprec)
312 {
313 /* Don't do unsigned arithmetic where signed was wanted,
314 or vice versa.
3cc247a8 315 Exception: if both of the original operands were
76e616db
BK
316 unsigned then can safely do the work as unsigned.
317 And we may need to do it as unsigned
318 if we truncate to the original size. */
ceef8ce4
NB
319 if (TREE_UNSIGNED (TREE_TYPE (expr))
320 || (TREE_UNSIGNED (TREE_TYPE (arg0))
321 && TREE_UNSIGNED (TREE_TYPE (arg1))))
322 typex = (*lang_hooks.types.unsigned_type) (typex);
323 else
324 typex = (*lang_hooks.types.signed_type) (typex);
76e616db 325 return convert (type,
95e78909
RK
326 fold (build (ex_form, typex,
327 convert (typex, arg0),
328 convert (typex, arg1),
329 0)));
76e616db
BK
330 }
331 }
332 }
333 break;
334
335 case NEGATE_EXPR:
336 case BIT_NOT_EXPR:
d283912a
RS
337 /* This is not correct for ABS_EXPR,
338 since we must test the sign before truncation. */
76e616db 339 {
b3694847 340 tree typex = type;
76e616db
BK
341
342 /* Can't do arithmetic in enumeral types
343 so use an integer type that will hold the values. */
344 if (TREE_CODE (typex) == ENUMERAL_TYPE)
b0c48229
NB
345 typex = (*lang_hooks.types.type_for_size)
346 (TYPE_PRECISION (typex), TREE_UNSIGNED (typex));
76e616db
BK
347
348 /* But now perhaps TYPEX is as wide as INPREC.
349 In that case, do nothing special here.
350 (Otherwise would recurse infinitely in convert. */
351 if (TYPE_PRECISION (typex) != inprec)
352 {
353 /* Don't do unsigned arithmetic where signed was wanted,
354 or vice versa. */
ceef8ce4
NB
355 if (TREE_UNSIGNED (TREE_TYPE (expr)))
356 typex = (*lang_hooks.types.unsigned_type) (typex);
357 else
358 typex = (*lang_hooks.types.signed_type) (typex);
76e616db 359 return convert (type,
95e78909
RK
360 fold (build1 (ex_form, typex,
361 convert (typex,
362 TREE_OPERAND (expr, 0)))));
76e616db
BK
363 }
364 }
365
366 case NOP_EXPR:
367 /* If truncating after truncating, might as well do all at once.
368 If truncating after extending, we may get rid of wasted work. */
369 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
370
371 case COND_EXPR:
f5963e61
JL
372 /* It is sometimes worthwhile to push the narrowing down through
373 the conditional and never loses. */
374 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
375 convert (type, TREE_OPERAND (expr, 1)),
376 convert (type, TREE_OPERAND (expr, 2))));
76e616db 377
31031edd
JL
378 default:
379 break;
76e616db
BK
380 }
381
382 return build1 (NOP_EXPR, type, expr);
76e616db 383
f5963e61
JL
384 case REAL_TYPE:
385 return build1 (FIX_TRUNC_EXPR, type, expr);
76e616db 386
f5963e61
JL
387 case COMPLEX_TYPE:
388 return convert (type,
389 fold (build1 (REALPART_EXPR,
390 TREE_TYPE (TREE_TYPE (expr)), expr)));
0b127821 391
0b4565c9
BS
392 case VECTOR_TYPE:
393 if (GET_MODE_SIZE (TYPE_MODE (type))
394 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
395 {
396 error ("can't convert between vector values of different size");
397 return error_mark_node;
398 }
399 return build1 (NOP_EXPR, type, expr);
400
f5963e61
JL
401 default:
402 error ("aggregate value used where an integer was expected");
403 return convert (type, integer_zero_node);
404 }
76e616db 405}
0b127821
RS
406
407/* Convert EXPR to the complex type TYPE in the usual ways. */
408
409tree
410convert_to_complex (type, expr)
411 tree type, expr;
412{
0b127821
RS
413 tree subtype = TREE_TYPE (type);
414
f5963e61 415 switch (TREE_CODE (TREE_TYPE (expr)))
0b127821 416 {
f5963e61
JL
417 case REAL_TYPE:
418 case INTEGER_TYPE:
419 case ENUMERAL_TYPE:
420 case BOOLEAN_TYPE:
421 case CHAR_TYPE:
422 return build (COMPLEX_EXPR, type, convert (subtype, expr),
0b127821 423 convert (subtype, integer_zero_node));
0b127821 424
f5963e61
JL
425 case COMPLEX_TYPE:
426 {
427 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
428
429 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
430 return expr;
431 else if (TREE_CODE (expr) == COMPLEX_EXPR)
0b127821
RS
432 return fold (build (COMPLEX_EXPR,
433 type,
f5963e61
JL
434 convert (subtype, TREE_OPERAND (expr, 0)),
435 convert (subtype, TREE_OPERAND (expr, 1))));
436 else
437 {
438 expr = save_expr (expr);
439 return
440 fold (build (COMPLEX_EXPR,
441 type, convert (subtype,
442 fold (build1 (REALPART_EXPR,
443 TREE_TYPE (TREE_TYPE (expr)),
444 expr))),
445 convert (subtype,
446 fold (build1 (IMAGPART_EXPR,
447 TREE_TYPE (TREE_TYPE (expr)),
448 expr)))));
449 }
450 }
0b127821 451
f5963e61
JL
452 case POINTER_TYPE:
453 case REFERENCE_TYPE:
454 error ("pointer value used where a complex was expected");
455 return convert_to_complex (type, integer_zero_node);
456
457 default:
458 error ("aggregate value used where a complex was expected");
459 return convert_to_complex (type, integer_zero_node);
460 }
0b127821 461}
0b4565c9
BS
462
463/* Convert EXPR to the vector type TYPE in the usual ways. */
464
465tree
466convert_to_vector (type, expr)
467 tree type, expr;
468{
0b4565c9
BS
469 switch (TREE_CODE (TREE_TYPE (expr)))
470 {
471 case INTEGER_TYPE:
472 case VECTOR_TYPE:
473 if (GET_MODE_SIZE (TYPE_MODE (type))
474 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
475 {
476 error ("can't convert between vector values of different size");
477 return error_mark_node;
478 }
479 return build1 (NOP_EXPR, type, expr);
480
481 default:
482 error ("can't convert value to a vector");
483 return convert_to_vector (type, integer_zero_node);
484 }
485}
This page took 0.912042 seconds and 5 git commands to generate.