Declare malloc, free, and atexit if inhibit_libc is defined.
[official-gcc.git] / gcc / convert.c
blob31ebb56ff3098aecb7a1c2c6798a9f959cb3a5d9
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"
30 #include "toplev.h"
32 /* Convert EXPR to some pointer or reference type TYPE.
34 EXPR must be pointer, reference, integer, enumeral, or literal zero;
35 in other cases error is called. */
37 tree
38 convert_to_pointer (type, expr)
39 tree type, expr;
41 if (integer_zerop (expr))
43 expr = build_int_2 (0, 0);
44 TREE_TYPE (expr) = type;
45 return expr;
48 switch (TREE_CODE (TREE_TYPE (expr)))
50 case POINTER_TYPE:
51 case REFERENCE_TYPE:
52 return build1 (NOP_EXPR, type, expr);
54 case INTEGER_TYPE:
55 case ENUMERAL_TYPE:
56 case BOOLEAN_TYPE:
57 case CHAR_TYPE:
58 if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
59 return build1 (CONVERT_EXPR, type, expr);
61 return
62 convert_to_pointer (type,
63 convert (type_for_size (POINTER_SIZE, 0), expr));
65 default:
66 error ("cannot convert to a pointer type");
67 return convert_to_pointer (type, integer_zero_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. */
76 tree
77 convert_to_real (type, expr)
78 tree type, expr;
80 switch (TREE_CODE (TREE_TYPE (expr)))
82 case REAL_TYPE:
83 return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
84 type, expr);
86 case INTEGER_TYPE:
87 case ENUMERAL_TYPE:
88 case BOOLEAN_TYPE:
89 case CHAR_TYPE:
90 return build1 (FLOAT_EXPR, type, expr);
92 case COMPLEX_TYPE:
93 return convert (type,
94 fold (build1 (REALPART_EXPR,
95 TREE_TYPE (TREE_TYPE (expr)), expr)));
97 case POINTER_TYPE:
98 case REFERENCE_TYPE:
99 error ("pointer value used where a floating point value was expected");
100 return convert_to_real (type, integer_zero_node);
102 default:
103 error ("aggregate value used where a float was expected");
104 return convert_to_real (type, integer_zero_node);
108 /* Convert EXPR to some integer (or enum) type TYPE.
110 EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
111 in other cases error is called.
113 The result of this is always supposed to be a newly created tree node
114 not in use in any existing structure. */
116 tree
117 convert_to_integer (type, expr)
118 tree type, expr;
120 enum tree_code ex_form = TREE_CODE (expr);
121 tree intype = TREE_TYPE (expr);
122 int inprec = TYPE_PRECISION (intype);
123 int outprec = TYPE_PRECISION (type);
125 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
126 be. Consider `enum E = { a, b = (enum E) 3 };'. */
127 if (!TYPE_SIZE (type))
129 error ("conversion to incomplete type");
130 return error_mark_node;
133 switch (TREE_CODE (intype))
135 case POINTER_TYPE:
136 case REFERENCE_TYPE:
137 if (integer_zerop (expr))
138 expr = integer_zero_node;
139 else
140 expr = fold (build1 (CONVERT_EXPR,
141 type_for_size (POINTER_SIZE, 0), expr));
143 return convert_to_integer (type, expr);
145 case INTEGER_TYPE:
146 case ENUMERAL_TYPE:
147 case BOOLEAN_TYPE:
148 case CHAR_TYPE:
149 /* If this is a logical operation, which just returns 0 or 1, we can
150 change the type of the expression. For some logical operations,
151 we must also change the types of the operands to maintain type
152 correctness. */
154 if (TREE_CODE_CLASS (ex_form) == '<')
156 TREE_TYPE (expr) = type;
157 return expr;
160 else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
161 || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
162 || ex_form == TRUTH_XOR_EXPR)
164 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
165 TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
166 TREE_TYPE (expr) = type;
167 return expr;
170 else if (ex_form == TRUTH_NOT_EXPR)
172 TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
173 TREE_TYPE (expr) = type;
174 return expr;
177 /* If we are widening the type, put in an explicit conversion.
178 Similarly if we are not changing the width. After this, we know
179 we are truncating EXPR. */
181 else if (outprec >= inprec)
182 return build1 (NOP_EXPR, type, expr);
184 /* If TYPE is an enumeral type or a type with a precision less
185 than the number of bits in its mode, do the conversion to the
186 type corresponding to its mode, then do a nop conversion
187 to TYPE. */
188 else if (TREE_CODE (type) == ENUMERAL_TYPE
189 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
190 return build1 (NOP_EXPR, type,
191 convert (type_for_mode (TYPE_MODE (type),
192 TREE_UNSIGNED (type)),
193 expr));
195 /* Here detect when we can distribute the truncation down past some
196 arithmetic. For example, if adding two longs and converting to an
197 int, we can equally well convert both to ints and then add.
198 For the operations handled here, such truncation distribution
199 is always safe.
200 It is desirable in these cases:
201 1) when truncating down to full-word from a larger size
202 2) when truncating takes no work.
203 3) when at least one operand of the arithmetic has been extended
204 (as by C's default conversions). In this case we need two conversions
205 if we do the arithmetic as already requested, so we might as well
206 truncate both and then combine. Perhaps that way we need only one.
208 Note that in general we cannot do the arithmetic in a type
209 shorter than the desired result of conversion, even if the operands
210 are both extended from a shorter type, because they might overflow
211 if combined in that type. The exceptions to this--the times when
212 two narrow values can be combined in their narrow type even to
213 make a wider result--are handled by "shorten" in build_binary_op. */
215 switch (ex_form)
217 case RSHIFT_EXPR:
218 /* We can pass truncation down through right shifting
219 when the shift count is a nonpositive constant. */
220 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
221 && tree_int_cst_lt (TREE_OPERAND (expr, 1),
222 convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
223 integer_one_node)))
224 goto trunc1;
225 break;
227 case LSHIFT_EXPR:
228 /* We can pass truncation down through left shifting
229 when the shift count is a nonnegative constant. */
230 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
231 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
232 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
234 /* If shift count is less than the width of the truncated type,
235 really shift. */
236 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
237 /* In this case, shifting is like multiplication. */
238 goto trunc1;
239 else
241 /* If it is >= that width, result is zero.
242 Handling this with trunc1 would give the wrong result:
243 (int) ((long long) a << 32) is well defined (as 0)
244 but (int) a << 32 is undefined and would get a
245 warning. */
247 tree t = convert_to_integer (type, integer_zero_node);
249 /* If the original expression had side-effects, we must
250 preserve it. */
251 if (TREE_SIDE_EFFECTS (expr))
252 return build (COMPOUND_EXPR, type, expr, t);
253 else
254 return t;
257 break;
259 case MAX_EXPR:
260 case MIN_EXPR:
261 case MULT_EXPR:
263 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
264 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
266 /* Don't distribute unless the output precision is at least as big
267 as the actual inputs. Otherwise, the comparison of the
268 truncated values will be wrong. */
269 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
270 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
271 /* If signedness of arg0 and arg1 don't match,
272 we can't necessarily find a type to compare them in. */
273 && (TREE_UNSIGNED (TREE_TYPE (arg0))
274 == TREE_UNSIGNED (TREE_TYPE (arg1))))
275 goto trunc1;
276 break;
279 case PLUS_EXPR:
280 case MINUS_EXPR:
281 case BIT_AND_EXPR:
282 case BIT_IOR_EXPR:
283 case BIT_XOR_EXPR:
284 case BIT_ANDTC_EXPR:
285 trunc1:
287 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
288 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
290 if (outprec >= BITS_PER_WORD
291 || TRULY_NOOP_TRUNCATION (outprec, inprec)
292 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
293 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
295 /* Do the arithmetic in type TYPEX,
296 then convert result to TYPE. */
297 register tree typex = type;
299 /* Can't do arithmetic in enumeral types
300 so use an integer type that will hold the values. */
301 if (TREE_CODE (typex) == ENUMERAL_TYPE)
302 typex = type_for_size (TYPE_PRECISION (typex),
303 TREE_UNSIGNED (typex));
305 /* But now perhaps TYPEX is as wide as INPREC.
306 In that case, do nothing special here.
307 (Otherwise would recurse infinitely in convert. */
308 if (TYPE_PRECISION (typex) != inprec)
310 /* Don't do unsigned arithmetic where signed was wanted,
311 or vice versa.
312 Exception: if either of the original operands were
313 unsigned then can safely do the work as unsigned.
314 And we may need to do it as unsigned
315 if we truncate to the original size. */
316 typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
317 || TREE_UNSIGNED (TREE_TYPE (arg0))
318 || TREE_UNSIGNED (TREE_TYPE (arg1)))
319 ? unsigned_type (typex) : signed_type (typex));
320 return convert (type,
321 fold (build (ex_form, typex,
322 convert (typex, arg0),
323 convert (typex, arg1),
324 0)));
328 break;
330 case NEGATE_EXPR:
331 case BIT_NOT_EXPR:
332 /* This is not correct for ABS_EXPR,
333 since we must test the sign before truncation. */
335 register tree typex = type;
337 /* Can't do arithmetic in enumeral types
338 so use an integer type that will hold the values. */
339 if (TREE_CODE (typex) == ENUMERAL_TYPE)
340 typex = type_for_size (TYPE_PRECISION (typex),
341 TREE_UNSIGNED (typex));
343 /* But now perhaps TYPEX is as wide as INPREC.
344 In that case, do nothing special here.
345 (Otherwise would recurse infinitely in convert. */
346 if (TYPE_PRECISION (typex) != inprec)
348 /* Don't do unsigned arithmetic where signed was wanted,
349 or vice versa. */
350 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
351 ? unsigned_type (typex) : signed_type (typex));
352 return convert (type,
353 fold (build1 (ex_form, typex,
354 convert (typex,
355 TREE_OPERAND (expr, 0)))));
359 case NOP_EXPR:
360 /* If truncating after truncating, might as well do all at once.
361 If truncating after extending, we may get rid of wasted work. */
362 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
364 case COND_EXPR:
365 /* It is sometimes worthwhile to push the narrowing down through
366 the conditional and never loses. */
367 return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
368 convert (type, TREE_OPERAND (expr, 1)),
369 convert (type, TREE_OPERAND (expr, 2))));
371 default:
372 break;
375 return build1 (NOP_EXPR, type, expr);
377 case REAL_TYPE:
378 return build1 (FIX_TRUNC_EXPR, type, expr);
380 case COMPLEX_TYPE:
381 return convert (type,
382 fold (build1 (REALPART_EXPR,
383 TREE_TYPE (TREE_TYPE (expr)), expr)));
385 default:
386 error ("aggregate value used where an integer was expected");
387 return convert (type, integer_zero_node);
391 /* Convert EXPR to the complex type TYPE in the usual ways. */
393 tree
394 convert_to_complex (type, expr)
395 tree type, expr;
397 tree subtype = TREE_TYPE (type);
399 switch (TREE_CODE (TREE_TYPE (expr)))
401 case REAL_TYPE:
402 case INTEGER_TYPE:
403 case ENUMERAL_TYPE:
404 case BOOLEAN_TYPE:
405 case CHAR_TYPE:
406 return build (COMPLEX_EXPR, type, convert (subtype, expr),
407 convert (subtype, integer_zero_node));
409 case COMPLEX_TYPE:
411 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
413 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
414 return expr;
415 else if (TREE_CODE (expr) == COMPLEX_EXPR)
416 return fold (build (COMPLEX_EXPR,
417 type,
418 convert (subtype, TREE_OPERAND (expr, 0)),
419 convert (subtype, TREE_OPERAND (expr, 1))));
420 else
422 expr = save_expr (expr);
423 return
424 fold (build (COMPLEX_EXPR,
425 type, convert (subtype,
426 fold (build1 (REALPART_EXPR,
427 TREE_TYPE (TREE_TYPE (expr)),
428 expr))),
429 convert (subtype,
430 fold (build1 (IMAGPART_EXPR,
431 TREE_TYPE (TREE_TYPE (expr)),
432 expr)))));
436 case POINTER_TYPE:
437 case REFERENCE_TYPE:
438 error ("pointer value used where a complex was expected");
439 return convert_to_complex (type, integer_zero_node);
441 default:
442 error ("aggregate value used where a complex was expected");
443 return convert_to_complex (type, integer_zero_node);