]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gcc/convert.c
ELF low-level toolchain bits now live in /usr/bin, not /usr/libexec/elf.
[FreeBSD/FreeBSD.git] / contrib / gcc / convert.c
1 /* Utility routines for data type conversion for GNU C.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997,
3    1998 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 /* These routines are somewhat language-independent utility function
24    intended to be called by the language-specific convert () functions.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "toplev.h"
32
33 /* Convert EXPR to some pointer or reference type TYPE.
34
35    EXPR must be pointer, reference, integer, enumeral, or literal zero;
36    in other cases error is called.  */
37
38 tree
39 convert_to_pointer (type, expr)
40      tree type, expr;
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   switch (TREE_CODE (TREE_TYPE (expr)))
50     {
51     case POINTER_TYPE:
52     case REFERENCE_TYPE:
53       return build1 (NOP_EXPR, type, expr);
54
55     case INTEGER_TYPE:
56     case ENUMERAL_TYPE:
57     case BOOLEAN_TYPE:
58     case CHAR_TYPE:
59       if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
60         return build1 (CONVERT_EXPR, type, expr);
61
62       return
63         convert_to_pointer (type,
64                             convert (type_for_size (POINTER_SIZE, 0), expr));
65
66     default:
67       error ("cannot convert to a pointer type");
68       return convert_to_pointer (type, integer_zero_node);
69     }
70 }
71
72 /* Convert EXPR to some floating-point type TYPE.
73
74    EXPR must be float, integer, or enumeral;
75    in other cases error is called.  */
76
77 tree
78 convert_to_real (type, expr)
79      tree type, expr;
80 {
81   switch (TREE_CODE (TREE_TYPE (expr)))
82     {
83     case REAL_TYPE:
84       return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
85                      type, expr);
86
87     case INTEGER_TYPE:
88     case ENUMERAL_TYPE:
89     case BOOLEAN_TYPE:
90     case CHAR_TYPE:
91       return build1 (FLOAT_EXPR, type, expr);
92
93     case COMPLEX_TYPE:
94       return convert (type,
95                       fold (build1 (REALPART_EXPR,
96                                     TREE_TYPE (TREE_TYPE (expr)), expr)));
97
98     case POINTER_TYPE:
99     case REFERENCE_TYPE:
100       error ("pointer value used where a floating point value was expected");
101       return convert_to_real (type, integer_zero_node);
102
103     default:
104       error ("aggregate value used where a float was expected");
105       return convert_to_real (type, integer_zero_node);
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), float, or
112    vector; 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   enum tree_code ex_form = TREE_CODE (expr);
122   tree intype = TREE_TYPE (expr);
123   unsigned int inprec = TYPE_PRECISION (intype);
124   unsigned int outprec = TYPE_PRECISION (type);
125
126   /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
127      be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
128   if (!COMPLETE_TYPE_P (type))
129     {
130       error ("conversion to incomplete type");
131       return error_mark_node;
132     }
133
134   switch (TREE_CODE (intype))
135     {
136     case POINTER_TYPE:
137     case REFERENCE_TYPE:
138       if (integer_zerop (expr))
139         expr = integer_zero_node;
140       else
141         expr = fold (build1 (CONVERT_EXPR,
142                              type_for_size (POINTER_SIZE, 0), expr));
143
144       return convert_to_integer (type, expr);
145
146     case INTEGER_TYPE:
147     case ENUMERAL_TYPE:
148     case BOOLEAN_TYPE:
149     case CHAR_TYPE:
150       /* If this is a logical operation, which just returns 0 or 1, we can
151          change the type of the expression.  For some logical operations,
152          we must also change the types of the operands to maintain type
153          correctness.  */
154
155       if (TREE_CODE_CLASS (ex_form) == '<')
156         {
157           TREE_TYPE (expr) = type;
158           return expr;
159         }
160
161       else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
162                || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
163                || ex_form == TRUTH_XOR_EXPR)
164         {
165           TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
166           TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
167           TREE_TYPE (expr) = type;
168           return expr;
169         }
170
171       else if (ex_form == TRUTH_NOT_EXPR)
172         {
173           TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
174           TREE_TYPE (expr) = type;
175           return expr;
176         }
177
178       /* If we are widening the type, put in an explicit conversion.
179          Similarly if we are not changing the width.  After this, we know
180          we are truncating EXPR.  */
181
182       else if (outprec >= inprec)
183         return build1 (NOP_EXPR, type, expr);
184
185       /* If TYPE is an enumeral type or a type with a precision less
186          than the number of bits in its mode, do the conversion to the
187          type corresponding to its mode, then do a nop conversion
188          to TYPE.  */
189       else if (TREE_CODE (type) == ENUMERAL_TYPE
190                || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
191         return build1 (NOP_EXPR, type,
192                        convert (type_for_mode (TYPE_MODE (type),
193                                                TREE_UNSIGNED (type)),
194                                 expr));
195
196       /* Here detect when we can distribute the truncation down past some
197          arithmetic.  For example, if adding two longs and converting to an
198          int, we can equally well convert both to ints and then add.
199          For the operations handled here, such truncation distribution
200          is always safe.
201          It is desirable in these cases:
202          1) when truncating down to full-word from a larger size
203          2) when truncating takes no work.
204          3) when at least one operand of the arithmetic has been extended
205          (as by C's default conversions).  In this case we need two conversions
206          if we do the arithmetic as already requested, so we might as well
207          truncate both and then combine.  Perhaps that way we need only one.
208
209          Note that in general we cannot do the arithmetic in a type
210          shorter than the desired result of conversion, even if the operands
211          are both extended from a shorter type, because they might overflow
212          if combined in that type.  The exceptions to this--the times when
213          two narrow values can be combined in their narrow type even to
214          make a wider result--are handled by "shorten" in build_binary_op.  */
215
216       switch (ex_form)
217         {
218         case RSHIFT_EXPR:
219           /* We can pass truncation down through right shifting
220              when the shift count is a nonpositive constant.  */
221           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
222               && tree_int_cst_lt (TREE_OPERAND (expr, 1),
223                                   convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
224                                            integer_one_node)))
225             goto trunc1;
226           break;
227
228         case LSHIFT_EXPR:
229           /* We can pass truncation down through left shifting
230              when the shift count is a nonnegative constant and
231              the target type is unsigned.  */
232           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
233               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
234               && TREE_UNSIGNED (type)
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
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                 }
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.  */
300                 tree typex = type;
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)
305                   typex = type_for_size (TYPE_PRECISION (typex),
306                                          TREE_UNSIGNED (typex));
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.
315                        Exception: if both of the original operands were
316                        unsigned then we can safely do the work as unsigned;
317                        if we are distributing through a LSHIFT_EXPR, we must
318                        do the work as unsigned to avoid a signed overflow.
319                        And we may need to do it as unsigned
320                        if we truncate to the original size.  */
321                     typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
322                               || (TREE_UNSIGNED (TREE_TYPE (arg0))
323                                   && TREE_UNSIGNED (TREE_TYPE (arg1)))
324                               || ex_form == LSHIFT_EXPR)
325                              ? unsigned_type (typex) : signed_type (typex));
326                     return convert (type,
327                                     fold (build (ex_form, typex,
328                                                  convert (typex, arg0),
329                                                  convert (typex, arg1),
330                                                  0)));
331                   }
332               }
333           }
334           break;
335
336         case NEGATE_EXPR:
337         case BIT_NOT_EXPR:
338           /* This is not correct for ABS_EXPR,
339              since we must test the sign before truncation.  */
340           {
341             tree typex = type;
342
343             /* Can't do arithmetic in enumeral types
344                so use an integer type that will hold the values.  */
345             if (TREE_CODE (typex) == ENUMERAL_TYPE)
346               typex = type_for_size (TYPE_PRECISION (typex),
347                                      TREE_UNSIGNED (typex));
348
349             /* But now perhaps TYPEX is as wide as INPREC.
350                In that case, do nothing special here.
351                (Otherwise would recurse infinitely in convert.  */
352             if (TYPE_PRECISION (typex) != inprec)
353               {
354                 /* Don't do unsigned arithmetic where signed was wanted,
355                    or vice versa.  */
356                 typex = (TREE_UNSIGNED (TREE_TYPE (expr))
357                          ? unsigned_type (typex) : signed_type (typex));
358                 return convert (type,
359                                 fold (build1 (ex_form, typex,
360                                               convert (typex,
361                                                        TREE_OPERAND (expr, 0)))));
362               }
363           }
364
365         case NOP_EXPR:
366           /* If truncating after truncating, might as well do all at once.
367              If truncating after extending, we may get rid of wasted work.  */
368           return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
369
370         case COND_EXPR:
371           /* It is sometimes worthwhile to push the narrowing down through
372              the conditional and never loses.  */
373           return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
374                               convert (type, TREE_OPERAND (expr, 1)), 
375                               convert (type, TREE_OPERAND (expr, 2))));
376
377         default:
378           break;
379         }
380
381       return build1 (NOP_EXPR, type, expr);
382
383     case REAL_TYPE:
384       return build1 (FIX_TRUNC_EXPR, type, expr);
385
386     case COMPLEX_TYPE:
387       return convert (type,
388                       fold (build1 (REALPART_EXPR,
389                                     TREE_TYPE (TREE_TYPE (expr)), expr)));
390
391     case VECTOR_TYPE:
392       if (GET_MODE_SIZE (TYPE_MODE (type))
393           != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
394         {
395           error ("can't convert between vector values of different size");
396           return error_mark_node;
397         }
398       return build1 (NOP_EXPR, type, expr);
399
400     default:
401       error ("aggregate value used where an integer was expected");
402       return convert (type, integer_zero_node);
403     }
404 }
405
406 /* Convert EXPR to the complex type TYPE in the usual ways.  */
407
408 tree
409 convert_to_complex (type, expr)
410      tree type, expr;
411 {
412   tree subtype = TREE_TYPE (type);
413   
414   switch (TREE_CODE (TREE_TYPE (expr)))
415     {
416     case REAL_TYPE:
417     case INTEGER_TYPE:
418     case ENUMERAL_TYPE:
419     case BOOLEAN_TYPE:
420     case CHAR_TYPE:
421       return build (COMPLEX_EXPR, type, convert (subtype, expr),
422                     convert (subtype, integer_zero_node));
423
424     case COMPLEX_TYPE:
425       {
426         tree elt_type = TREE_TYPE (TREE_TYPE (expr));
427
428         if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
429           return expr;
430         else if (TREE_CODE (expr) == COMPLEX_EXPR)
431           return fold (build (COMPLEX_EXPR,
432                               type,
433                               convert (subtype, TREE_OPERAND (expr, 0)),
434                               convert (subtype, TREE_OPERAND (expr, 1))));
435         else
436           {
437             expr = save_expr (expr);
438             return
439               fold (build (COMPLEX_EXPR,
440                            type, convert (subtype,
441                                           fold (build1 (REALPART_EXPR,
442                                                         TREE_TYPE (TREE_TYPE (expr)),
443                                                         expr))),
444                            convert (subtype,
445                                     fold (build1 (IMAGPART_EXPR,
446                                                   TREE_TYPE (TREE_TYPE (expr)),
447                                                   expr)))));
448           }
449       }
450
451     case POINTER_TYPE:
452     case REFERENCE_TYPE:
453       error ("pointer value used where a complex was expected");
454       return convert_to_complex (type, integer_zero_node);
455
456     default:
457       error ("aggregate value used where a complex was expected");
458       return convert_to_complex (type, integer_zero_node);
459     }
460 }
461
462 /* Convert EXPR to the vector type TYPE in the usual ways.  */
463
464 tree
465 convert_to_vector (type, expr)
466      tree type, expr;
467 {
468   switch (TREE_CODE (TREE_TYPE (expr)))
469     {
470     case INTEGER_TYPE:
471     case VECTOR_TYPE:
472       if (GET_MODE_SIZE (TYPE_MODE (type))
473           != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))))
474         {
475           error ("can't convert between vector values of different size");
476           return error_mark_node;
477         }
478       return build1 (NOP_EXPR, type, expr);
479
480     default:
481       error ("can't convert value to a vector");
482       return convert_to_vector (type, integer_zero_node);
483     }
484 }