OSDN Git Service

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