OSDN Git Service

Update FSF address.
[pf3gnuchains/gcc-fork.git] / gcc / convert.c
1 /* Utility routines for data type conversion for GNU C.
2    Copyright (C) 1987, 1988, 1991, 1992, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU C.
5
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.
10
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.
15
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.  */
20
21
22 /* These routines are somewhat language-independent utility function
23    intended to be called by the language-specific convert () functions. */
24
25 #include "config.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "convert.h"
29
30 /* Convert EXPR to some pointer or reference type TYPE.
31
32    EXPR must be pointer, reference, integer, enumeral, or literal zero;
33    in other cases error is called. */
34
35 tree
36 convert_to_pointer (type, expr)
37      tree type, expr;
38 {
39   register tree intype = TREE_TYPE (expr);
40   register enum tree_code form = TREE_CODE (intype);
41   
42   if (integer_zerop (expr))
43     {
44       expr = build_int_2 (0, 0);
45       TREE_TYPE (expr) = type;
46       return expr;
47     }
48
49   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
50     return build1 (NOP_EXPR, type, expr);
51
52
53   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
54     {
55       if (type_precision (intype) == POINTER_SIZE)
56         return build1 (CONVERT_EXPR, type, expr);
57       expr = convert (type_for_size (POINTER_SIZE, 0), expr);
58       /* Modes may be different but sizes should be the same.  */
59       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
60           != GET_MODE_SIZE (TYPE_MODE (type)))
61         /* There is supposed to be some integral type
62            that is the same width as a pointer.  */
63         abort ();
64       return convert_to_pointer (type, expr);
65     }
66
67   error ("cannot convert to a pointer type");
68
69   expr = build_int_2 (0, 0);
70   TREE_TYPE (expr) = type;
71   return expr;
72 }
73
74 /* Convert EXPR to some floating-point type TYPE.
75
76    EXPR must be float, integer, or enumeral;
77    in other cases error is called. */
78
79 tree
80 convert_to_real (type, expr)
81      tree type, expr;
82 {
83   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
84
85   if (form == REAL_TYPE)
86     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
87                    type, expr);
88
89   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
90     return build1 (FLOAT_EXPR, type, expr);
91
92   if (form == COMPLEX_TYPE)
93     return convert (type, fold (build1 (REALPART_EXPR,
94                                         TREE_TYPE (TREE_TYPE (expr)), expr)));
95
96   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
97     error ("pointer value used where a floating point value was expected");
98   else
99     error ("aggregate value used where a float was expected");
100
101   {
102     register tree tem = make_node (REAL_CST);
103     TREE_TYPE (tem) = type;
104     TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0", TYPE_MODE (type));
105     return tem;
106   }
107 }
108
109 /* Convert EXPR to some integer (or enum) type TYPE.
110
111    EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
112    in other cases error is called.
113
114    The result of this is always supposed to be a newly created tree node
115    not in use in any existing structure.  */
116
117 tree
118 convert_to_integer (type, expr)
119      tree type, expr;
120 {
121   register tree intype = TREE_TYPE (expr);
122   register enum tree_code form = TREE_CODE (intype);
123
124   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
125     {
126       if (integer_zerop (expr))
127         expr = integer_zero_node;
128       else
129         expr = fold (build1 (CONVERT_EXPR,
130                              type_for_size (POINTER_SIZE, 0), expr));
131       intype = TREE_TYPE (expr);
132       form = TREE_CODE (intype);
133       if (intype == type)
134         return expr;
135     }
136
137   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE
138       || form == BOOLEAN_TYPE || form == CHAR_TYPE)
139     {
140       register unsigned outprec = TYPE_PRECISION (type);
141       register unsigned inprec = TYPE_PRECISION (intype);
142       register enum tree_code ex_form = TREE_CODE (expr);
143
144       /* If we are widening the type, put in an explicit conversion.
145          Similarly if we are not changing the width.  However, if this is
146          a logical operation that just returns 0 or 1, we can change the
147          type of the expression.  For logical operations, we must
148          also change the types of the operands to maintain type
149          correctness.  */
150
151       if (TREE_CODE_CLASS (ex_form) == '<')
152         {
153           TREE_TYPE (expr) = type;
154           return expr;
155         }
156       else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
157                || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
158                || ex_form == TRUTH_XOR_EXPR)
159         {
160           TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
161           TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
162           TREE_TYPE (expr) = type;
163           return expr;
164         }
165       else if (ex_form == TRUTH_NOT_EXPR)
166         {
167           TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
168           TREE_TYPE (expr) = type;
169           return expr;
170         }
171       else if (outprec >= inprec)
172         return build1 (NOP_EXPR, type, expr);
173
174       /* Here detect when we can distribute the truncation down past some
175          arithmetic.  For example, if adding two longs and converting to an
176          int, we can equally well convert both to ints and then add.
177          For the operations handled here, such truncation distribution
178          is always safe.
179          It is desirable in these cases:
180          1) when truncating down to full-word from a larger size
181          2) when truncating takes no work.
182          3) when at least one operand of the arithmetic has been extended
183          (as by C's default conversions).  In this case we need two conversions
184          if we do the arithmetic as already requested, so we might as well
185          truncate both and then combine.  Perhaps that way we need only one.
186
187          Note that in general we cannot do the arithmetic in a type
188          shorter than the desired result of conversion, even if the operands
189          are both extended from a shorter type, because they might overflow
190          if combined in that type.  The exceptions to this--the times when
191          two narrow values can be combined in their narrow type even to
192          make a wider result--are handled by "shorten" in build_binary_op.  */
193
194       switch (ex_form)
195         {
196         case RSHIFT_EXPR:
197           /* We can pass truncation down through right shifting
198              when the shift count is a nonpositive constant.  */
199           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
200               && tree_int_cst_lt (TREE_OPERAND (expr, 1),
201                                   convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
202                                            integer_one_node)))
203             goto trunc1;
204           break;
205
206         case LSHIFT_EXPR:
207           /* We can pass truncation down through left shifting
208              when the shift count is a nonnegative constant.  */
209           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
210               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
211               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
212             {
213               /* If shift count is less than the width of the truncated type,
214                  really shift.  */
215               if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
216                 /* In this case, shifting is like multiplication.  */
217                 goto trunc1;
218               else
219                 {
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
224                      warning.  */
225
226                   tree t = convert_to_integer (type, integer_zero_node);
227
228                   /* If the original expression had side-effects, we must
229                      preserve it.  */
230                   if (TREE_SIDE_EFFECTS (expr))
231                     return build (COMPOUND_EXPR, type, expr, t);
232                   else
233                     return t;
234                 }
235             }
236           break;
237
238         case MAX_EXPR:
239         case MIN_EXPR:
240         case MULT_EXPR:
241           {
242             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
243             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
244
245             /* Don't distribute unless the output precision is at least as big
246                as the actual inputs.  Otherwise, the comparison of the
247                truncated values will be wrong.  */
248             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
249                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
250                 /* If signedness of arg0 and arg1 don't match,
251                    we can't necessarily find a type to compare them in.  */
252                 && (TREE_UNSIGNED (TREE_TYPE (arg0))
253                     == TREE_UNSIGNED (TREE_TYPE (arg1))))
254               goto trunc1;
255             break;
256           }
257
258         case PLUS_EXPR:
259         case MINUS_EXPR:
260         case BIT_AND_EXPR:
261         case BIT_IOR_EXPR:
262         case BIT_XOR_EXPR:
263         case BIT_ANDTC_EXPR:
264         trunc1:
265           {
266             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
267             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
268
269             if (outprec >= BITS_PER_WORD
270                 || TRULY_NOOP_TRUNCATION (outprec, inprec)
271                 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
272                 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
273               {
274                 /* Do the arithmetic in type TYPEX,
275                    then convert result to TYPE.  */
276                 register tree typex = type;
277
278                 /* Can't do arithmetic in enumeral types
279                    so use an integer type that will hold the values.  */
280                 if (TREE_CODE (typex) == ENUMERAL_TYPE)
281                   typex = type_for_size (TYPE_PRECISION (typex),
282                                          TREE_UNSIGNED (typex));
283
284                 /* But now perhaps TYPEX is as wide as INPREC.
285                    In that case, do nothing special here.
286                    (Otherwise would recurse infinitely in convert.  */
287                 if (TYPE_PRECISION (typex) != inprec)
288                   {
289                     /* Don't do unsigned arithmetic where signed was wanted,
290                        or vice versa.
291                        Exception: if either of the original operands were
292                        unsigned then can safely do the work as unsigned.
293                        And we may need to do it as unsigned
294                        if we truncate to the original size.  */
295                     typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
296                               || TREE_UNSIGNED (TREE_TYPE (arg0))
297                               || TREE_UNSIGNED (TREE_TYPE (arg1)))
298                              ? unsigned_type (typex) : signed_type (typex));
299                     return convert (type,
300                                     fold (build (ex_form, typex,
301                                                  convert (typex, arg0),
302                                                  convert (typex, arg1),
303                                                  0)));
304                   }
305               }
306           }
307           break;
308
309         case NEGATE_EXPR:
310         case BIT_NOT_EXPR:
311           /* This is not correct for ABS_EXPR,
312              since we must test the sign before truncation.  */
313           {
314             register tree typex = type;
315
316             /* Can't do arithmetic in enumeral types
317                so use an integer type that will hold the values.  */
318             if (TREE_CODE (typex) == ENUMERAL_TYPE)
319               typex = type_for_size (TYPE_PRECISION (typex),
320                                      TREE_UNSIGNED (typex));
321
322             /* But now perhaps TYPEX is as wide as INPREC.
323                In that case, do nothing special here.
324                (Otherwise would recurse infinitely in convert.  */
325             if (TYPE_PRECISION (typex) != inprec)
326               {
327                 /* Don't do unsigned arithmetic where signed was wanted,
328                    or vice versa.  */
329                 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
330                          ? unsigned_type (typex) : signed_type (typex));
331                 return convert (type,
332                                 fold (build1 (ex_form, typex,
333                                               convert (typex,
334                                                        TREE_OPERAND (expr, 0)))));
335               }
336           }
337
338         case NOP_EXPR:
339           /* If truncating after truncating, might as well do all at once.
340              If truncating after extending, we may get rid of wasted work.  */
341           return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
342
343         case COND_EXPR:
344           /* Can treat the two alternative values like the operands
345              of an arithmetic expression.  */
346           {
347             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
348             tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
349
350             if (outprec >= BITS_PER_WORD
351                 || TRULY_NOOP_TRUNCATION (outprec, inprec)
352                 || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
353                 || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
354               {
355                 /* Do the arithmetic in type TYPEX,
356                    then convert result to TYPE.  */
357                 register tree typex = type;
358
359                 /* Can't do arithmetic in enumeral types
360                    so use an integer type that will hold the values.  */
361                 if (TREE_CODE (typex) == ENUMERAL_TYPE)
362                   typex = type_for_size (TYPE_PRECISION (typex),
363                                          TREE_UNSIGNED (typex));
364
365                 /* But now perhaps TYPEX is as wide as INPREC.
366                    In that case, do nothing special here.
367                    (Otherwise would recurse infinitely in convert.  */
368                 if (TYPE_PRECISION (typex) != inprec)
369                   {
370                     /* Don't do unsigned arithmetic where signed was wanted,
371                        or vice versa.  */
372                     typex = (TREE_UNSIGNED (TREE_TYPE (expr))
373                              ? unsigned_type (typex) : signed_type (typex));
374                     return convert (type,
375                                     fold (build (COND_EXPR, typex,
376                                                  TREE_OPERAND (expr, 0),
377                                                  convert (typex, arg1),
378                                                  convert (typex, arg2))));
379                   }
380                 else
381                   /* It is sometimes worthwhile
382                      to push the narrowing down through the conditional.  */
383                   return fold (build (COND_EXPR, type,
384                                       TREE_OPERAND (expr, 0),
385                                       convert (type, TREE_OPERAND (expr, 1)), 
386                                       convert (type, TREE_OPERAND (expr, 2))));
387               }
388           }
389
390         }
391
392       return build1 (NOP_EXPR, type, expr);
393     }
394
395   if (form == REAL_TYPE)
396     return build1 (FIX_TRUNC_EXPR, type, expr);
397
398   if (form == COMPLEX_TYPE)
399     return convert (type, fold (build1 (REALPART_EXPR,
400                                         TREE_TYPE (TREE_TYPE (expr)), expr)));
401
402   error ("aggregate value used where an integer was expected");
403
404   {
405     register tree tem = build_int_2 (0, 0);
406     TREE_TYPE (tem) = type;
407     return tem;
408   }
409 }
410
411 /* Convert EXPR to the complex type TYPE in the usual ways.  */
412
413 tree
414 convert_to_complex (type, expr)
415      tree type, expr;
416 {
417   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
418   tree subtype = TREE_TYPE (type);
419   
420   if (form == REAL_TYPE || form == INTEGER_TYPE || form == ENUMERAL_TYPE)
421     {
422       expr = convert (subtype, expr);
423       return build (COMPLEX_EXPR, type, expr,
424                     convert (subtype, integer_zero_node));
425     }
426
427   if (form == COMPLEX_TYPE)
428     {
429       tree elt_type = TREE_TYPE (TREE_TYPE (expr));
430       if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
431         return expr;
432       else if (TREE_CODE (expr) == COMPLEX_EXPR)
433         return fold (build (COMPLEX_EXPR,
434                             type,
435                             convert (subtype, TREE_OPERAND (expr, 0)),
436                             convert (subtype, TREE_OPERAND (expr, 1))));
437       else
438         {
439           expr = save_expr (expr);
440           return fold (build (COMPLEX_EXPR,
441                               type,
442                               convert (subtype,
443                                        fold (build1 (REALPART_EXPR,
444                                                      TREE_TYPE (TREE_TYPE (expr)),
445                                                      expr))),
446                               convert (subtype,
447                                        fold (build1 (IMAGPART_EXPR,
448                                                      TREE_TYPE (TREE_TYPE (expr)),
449                                                      expr)))));
450         }
451     }
452
453   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
454     error ("pointer value used where a complex was expected");
455   else
456     error ("aggregate value used where a complex was expected");
457   
458   return build (COMPLEX_EXPR, type,
459                 convert (subtype, integer_zero_node),
460                 convert (subtype, integer_zero_node));
461 }