OSDN Git Service

* config/i386/i386.md (builtin_setjmp_receiver): New pattern.
[pf3gnuchains/gcc-fork.git] / gcc / convert.c
1 /* Utility routines for data type conversion for GNU C.
2    Copyright (C) 1987, 88, 91-95, 97, 1998 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 "system.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "convert.h"
30 #include "toplev.h"
31
32 /* Convert EXPR to some pointer or reference type TYPE.
33
34    EXPR must be pointer, reference, integer, enumeral, or literal zero;
35    in other cases error is called.  */
36
37 tree
38 convert_to_pointer (type, expr)
39      tree type, expr;
40 {
41   if (integer_zerop (expr))
42     {
43       expr = build_int_2 (0, 0);
44       TREE_TYPE (expr) = type;
45       return expr;
46     }
47
48   switch (TREE_CODE (TREE_TYPE (expr)))
49     {
50     case POINTER_TYPE:
51     case REFERENCE_TYPE:
52       return build1 (NOP_EXPR, type, expr);
53
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);
60
61       return
62         convert_to_pointer (type,
63                             convert (type_for_size (POINTER_SIZE, 0), expr));
64
65     default:
66       error ("cannot convert to a pointer type");
67       return convert_to_pointer (type, integer_zero_node);
68     }
69 }
70
71 /* Convert EXPR to some floating-point type TYPE.
72
73    EXPR must be float, integer, or enumeral;
74    in other cases error is called.  */
75
76 tree
77 convert_to_real (type, expr)
78      tree type, expr;
79 {
80   switch (TREE_CODE (TREE_TYPE (expr)))
81     {
82     case REAL_TYPE:
83       return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
84                      type, expr);
85
86     case INTEGER_TYPE:
87     case ENUMERAL_TYPE:
88     case BOOLEAN_TYPE:
89     case CHAR_TYPE:
90       return build1 (FLOAT_EXPR, type, expr);
91
92     case COMPLEX_TYPE:
93       return convert (type,
94                       fold (build1 (REALPART_EXPR,
95                                     TREE_TYPE (TREE_TYPE (expr)), expr)));
96
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);
101
102     default:
103       error ("aggregate value used where a float was expected");
104       return convert_to_real (type, integer_zero_node);
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   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);
124
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))
128     {
129       error ("conversion to incomplete type");
130       return error_mark_node;
131     }
132
133   switch (TREE_CODE (intype))
134     {
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));
142
143       return convert_to_integer (type, expr);
144
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.  */
153
154       if (TREE_CODE_CLASS (ex_form) == '<')
155         {
156           TREE_TYPE (expr) = type;
157           return expr;
158         }
159
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)
163         {
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;
168         }
169
170       else if (ex_form == TRUTH_NOT_EXPR)
171         {
172           TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
173           TREE_TYPE (expr) = type;
174           return expr;
175         }
176
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.  */
180
181       else if (outprec >= inprec)
182         return build1 (NOP_EXPR, type, expr);
183
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));
194
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.
207
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.  */
214
215       switch (ex_form)
216         {
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;
226
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)
233             {
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
240                 {
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.  */
246
247                   tree t = convert_to_integer (type, integer_zero_node);
248
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;
255                 }
256             }
257           break;
258
259         case MAX_EXPR:
260         case MIN_EXPR:
261         case MULT_EXPR:
262           {
263             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
264             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
265
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;
277           }
278
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:
286           {
287             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
288             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
289
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)))
294               {
295                 /* Do the arithmetic in type TYPEX,
296                    then convert result to TYPE.  */
297                 register tree typex = type;
298
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));
304
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)
309                   {
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)));
325                   }
326               }
327           }
328           break;
329
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.  */
334           {
335             register tree typex = type;
336
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));
342
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)
347               {
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)))));
356               }
357           }
358
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));
363
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))));
370
371         default:
372           break;
373         }
374
375       return build1 (NOP_EXPR, type, expr);
376
377     case REAL_TYPE:
378       return build1 (FIX_TRUNC_EXPR, type, expr);
379
380     case COMPLEX_TYPE:
381       return convert (type,
382                       fold (build1 (REALPART_EXPR,
383                                     TREE_TYPE (TREE_TYPE (expr)), expr)));
384
385     default:
386       error ("aggregate value used where an integer was expected");
387       return convert (type, integer_zero_node);
388     }
389 }
390
391 /* Convert EXPR to the complex type TYPE in the usual ways.  */
392
393 tree
394 convert_to_complex (type, expr)
395      tree type, expr;
396 {
397   tree subtype = TREE_TYPE (type);
398   
399   switch (TREE_CODE (TREE_TYPE (expr)))
400     {
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));
408
409     case COMPLEX_TYPE:
410       {
411         tree elt_type = TREE_TYPE (TREE_TYPE (expr));
412
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
421           {
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)))));
433           }
434       }
435
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);
440
441     default:
442       error ("aggregate value used where a complex was expected");
443       return convert_to_complex (type, integer_zero_node);
444     }
445 }