home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources-C / c-convert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  13.7 KB  |  418 lines  |  [TEXT/MPS ]

  1. /* Language-level data type conversion for GNU C.
  2.    Copyright (C) 1987, 1988, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  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. /* This file contains the functions for converting C expressions
  22.    to different data types.  The only entry point is `convert'.
  23.    Every language front end must have a `convert' function
  24.    but what kind of conversions it does will depend on the language.  */
  25.  
  26. #include "config.h"
  27. #include "tree.h"
  28. #include "flags.h"
  29.  
  30. /* Change of width--truncation and extension of integers or reals--
  31.    is represented with NOP_EXPR.  Proper functioning of many things
  32.    assumes that no other conversions can be NOP_EXPRs.
  33.  
  34.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  35.    Converting integer to real uses FLOAT_EXPR
  36.    and real to integer uses FIX_TRUNC_EXPR.
  37.  
  38.    Here is a list of all the functions that assume that widening and
  39.    narrowing is always done with a NOP_EXPR:
  40.      In c-convert.c, convert_to_integer.
  41.      In c-typeck.c, build_binary_op (boolean ops), and truthvalue_conversion.
  42.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  43.      In fold-const.c: fold.
  44.      In tree.c: get_narrower and get_unwidened.  */
  45.  
  46. /* Subroutines of `convert'.  */
  47.  
  48. static tree
  49. convert_to_pointer (type, expr)
  50.      tree type, expr;
  51. {
  52.   register tree intype = TREE_TYPE (expr);
  53.   register enum tree_code form = TREE_CODE (intype);
  54.   
  55.   if (integer_zerop (expr))
  56.     {
  57.       if (type == TREE_TYPE (NULL_POINTER (DPmode)))
  58.     return NULL_POINTER (DPmode);
  59.       if (type == TREE_TYPE (NULL_POINTER (TPmode)))
  60.     return NULL_POINTER (TPmode);
  61.       expr = build_int_2 (0, 0);
  62.       TREE_TYPE (expr) = type;
  63.       return expr;
  64.     }
  65.  
  66.   if (form == POINTER_TYPE)
  67.     return build1 (NOP_EXPR, type, expr);
  68.  
  69.  
  70.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  71.     {
  72.       if (type_precision (intype) == POINTER_SIZE)
  73.     return build1 (CONVERT_EXPR, type, expr);
  74.       expr = convert (type_for_size (POINTER_SIZE, 0), expr);
  75.       if (TYPE_MODE (TREE_TYPE (expr)) != TYPE_MODE (type))
  76.     /* There is supposed to be some integral type
  77.        that is the same width as a pointer.  */
  78.     abort ();
  79.       return convert_to_pointer (type, expr);
  80.     }
  81.  
  82.   error ("cannot convert to a pointer type");
  83.  
  84.   return NULL_POINTER (DPmode);
  85. }
  86.  
  87. static tree
  88. convert_to_real (type, expr)
  89.      tree type, expr;
  90. {
  91.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  92.  
  93.   if (form == REAL_TYPE)
  94.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  95.            type, expr);
  96.  
  97.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  98.     return build1 (FLOAT_EXPR, type, expr);
  99.  
  100.   if (form == POINTER_TYPE)
  101.     error ("pointer value used where a float was expected");
  102.   else
  103.     error ("aggregate value used where a float was expected");
  104.  
  105.   {
  106.     register tree tem = make_node (REAL_CST);
  107.     TREE_TYPE (tem) = type;
  108.     TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0");
  109.     return tem;
  110.   }
  111. }
  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. static 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.     {
  138.       register unsigned outprec = TYPE_PRECISION (type);
  139.       register unsigned inprec = TYPE_PRECISION (intype);
  140.       register enum tree_code ex_form = TREE_CODE (expr);
  141.  
  142.       /* If we are widening the type, put in an explicit conversion.
  143.      Similarly if we are not changing the width.  However, if this is
  144.      a logical operation that just returns 0 or 1, we can change the
  145.      type of the expression (see below).  */
  146.  
  147.       if (TREE_CODE_CLASS (ex_form) == '<'
  148.       || ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
  149.       || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
  150.       || ex_form == TRUTH_NOT_EXPR)
  151.     {
  152.       TREE_TYPE (expr) = type;
  153.       return expr;
  154.     }
  155.       else if (outprec >= inprec)
  156.     return build1 (NOP_EXPR, type, expr);
  157.  
  158. /* Here detect when we can distribute the truncation down past some arithmetic.
  159.    For example, if adding two longs and converting to an int,
  160.    we can equally well convert both to ints and then add.
  161.    For the operations handled here, such truncation distribution
  162.    is always safe.
  163.    It is desirable in these cases:
  164.    1) when truncating down to full-word from a larger size
  165.    2) when truncating takes no work.
  166.    3) when at least one operand of the arithmetic has been extended
  167.    (as by C's default conversions).  In this case we need two conversions
  168.    if we do the arithmetic as already requested, so we might as well
  169.    truncate both and then combine.  Perhaps that way we need only one.
  170.  
  171.    Note that in general we cannot do the arithmetic in a type
  172.    shorter than the desired result of conversion, even if the operands
  173.    are both extended from a shorter type, because they might overflow
  174.    if combined in that type.  The exceptions to this--the times when
  175.    two narrow values can be combined in their narrow type even to
  176.    make a wider result--are handled by "shorten" in build_binary_op.  */
  177.  
  178.       switch (ex_form)
  179.     {
  180.     case RSHIFT_EXPR:
  181.       /* We can pass truncation down through right shifting
  182.          when the shift count is a nonpositive constant.  */
  183.       if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  184.           && tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_one_node))
  185.         goto trunc1;
  186.       break;
  187.  
  188.     case LSHIFT_EXPR:
  189.       /* We can pass truncation down through left shifting
  190.          when the shift count is a nonnegative constant.  */
  191.       if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  192.           && ! tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_zero_node)
  193.           && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  194.         {
  195.           /* If shift count is less than the width of the truncated type,
  196.          really shift.  */
  197.           if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
  198.         /* In this case, shifting is like multiplication.  */
  199.         goto trunc1;
  200.           else
  201.         /* If it is >= that width, result is zero.
  202.            Handling this with trunc1 would give the wrong result:
  203.            (int) ((long long) a << 32) is well defined (as 0)
  204.            but (int) a << 32 is undefined and would get a warning.  */
  205.         return convert_to_integer (type, integer_zero_node);
  206.         }
  207.       break;
  208.  
  209.     case MAX_EXPR:
  210.     case MIN_EXPR:
  211.     case MULT_EXPR:
  212.       {
  213.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  214.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  215.  
  216.         /* Don't distribute unless the output precision is at least as big
  217.            as the actual inputs.  Otherwise, the comparison of the
  218.            truncated values will be wrong.  */
  219.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  220.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  221.         /* If signedness of arg0 and arg1 don't match,
  222.            we can't necessarily find a type to compare them in.  */
  223.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  224.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  225.           goto trunc1;
  226.         break;
  227.       }
  228.  
  229.     case PLUS_EXPR:
  230.     case MINUS_EXPR:
  231.     case BIT_AND_EXPR:
  232.     case BIT_IOR_EXPR:
  233.     case BIT_XOR_EXPR:
  234.     case BIT_ANDTC_EXPR:
  235.     trunc1:
  236.       {
  237.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  238.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  239.  
  240.         if (outprec >= BITS_PER_WORD
  241.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  242.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  243.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  244.           {
  245.         /* Do the arithmetic in type TYPEX,
  246.            then convert result to TYPE.  */
  247.         register tree typex = type;
  248.  
  249.         /* Can't do arithmetic in enumeral types
  250.            so use an integer type that will hold the values.  */
  251.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  252.           typex = type_for_size (TYPE_PRECISION (typex),
  253.                      TREE_UNSIGNED (typex));
  254.  
  255.         /* But now perhaps TYPEX is as wide as INPREC.
  256.            In that case, do nothing special here.
  257.            (Otherwise would recurse infinitely in convert.  */
  258.         if (TYPE_PRECISION (typex) != inprec)
  259.           {
  260.             /* Don't do unsigned arithmetic where signed was wanted,
  261.                or vice versa.
  262.                Exception: if either of the original operands were
  263.                unsigned then can safely do the work as unsigned.
  264.                And we may need to do it as unsigned
  265.                if we truncate to the original size.  */
  266.             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  267.                   || TREE_UNSIGNED (TREE_TYPE (arg0))
  268.                   || TREE_UNSIGNED (TREE_TYPE (arg1)))
  269.                  ? unsigned_type (typex) : signed_type (typex));
  270.             return convert (type,
  271.                     build_binary_op (ex_form,
  272.                              convert (typex, arg0),
  273.                              convert (typex, arg1),
  274.                              0));
  275.           }
  276.           }
  277.       }
  278.       break;
  279.  
  280.     case NEGATE_EXPR:
  281.     case BIT_NOT_EXPR:
  282.       {
  283.         register tree typex = type;
  284.  
  285.         /* Can't do arithmetic in enumeral types
  286.            so use an integer type that will hold the values.  */
  287.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  288.           typex = type_for_size (TYPE_PRECISION (typex),
  289.                      TREE_UNSIGNED (typex));
  290.  
  291.         /* But now perhaps TYPEX is as wide as INPREC.
  292.            In that case, do nothing special here.
  293.            (Otherwise would recurse infinitely in convert.  */
  294.         if (TYPE_PRECISION (typex) != inprec)
  295.           {
  296.         /* Don't do unsigned arithmetic where signed was wanted,
  297.            or vice versa.  */
  298.         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  299.              ? unsigned_type (typex) : signed_type (typex));
  300.         return convert (type,
  301.                 build_unary_op (ex_form,
  302.                         convert (typex, TREE_OPERAND (expr, 0)),
  303.                         1));
  304.           }
  305.       }
  306.  
  307.     case NOP_EXPR:
  308.       /* If truncating after truncating, might as well do all at once.
  309.          If truncating after extending, we may get rid of wasted work.  */
  310.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  311.  
  312.     case COND_EXPR:
  313.       /* Can treat the two alternative values like the operands
  314.          of an arithmetic expression.  */
  315.       {
  316.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  317.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  318.  
  319.         if (outprec >= BITS_PER_WORD
  320.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  321.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  322.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  323.           {
  324.         /* Do the arithmetic in type TYPEX,
  325.            then convert result to TYPE.  */
  326.         register tree typex = type;
  327.  
  328.         /* Can't do arithmetic in enumeral types
  329.            so use an integer type that will hold the values.  */
  330.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  331.           typex = type_for_size (TYPE_PRECISION (typex),
  332.                      TREE_UNSIGNED (typex));
  333.  
  334.         /* But now perhaps TYPEX is as wide as INPREC.
  335.            In that case, do nothing special here.
  336.            (Otherwise would recurse infinitely in convert.  */
  337.         if (TYPE_PRECISION (typex) != inprec)
  338.           {
  339.             /* Don't do unsigned arithmetic where signed was wanted,
  340.                or vice versa.  */
  341.             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  342.                  ? unsigned_type (typex) : signed_type (typex));
  343.             return convert (type,
  344.                     fold (build (COND_EXPR, typex,
  345.                          TREE_OPERAND (expr, 0),
  346.                          convert (typex, arg1),
  347.                          convert (typex, arg2))));
  348.           }
  349.         else
  350.           /* It is sometimes worthwhile
  351.              to push the narrowing down through the conditional.  */
  352.           return fold (build (COND_EXPR, type,
  353.                       TREE_OPERAND (expr, 0),
  354.                       convert (type, TREE_OPERAND (expr, 1)), 
  355.                       convert (type, TREE_OPERAND (expr, 2))));
  356.           }
  357.       }
  358.     }
  359.  
  360.       return build1 (NOP_EXPR, type, expr);
  361.     }
  362.  
  363.   if (form == REAL_TYPE)
  364.     return build1 (FIX_TRUNC_EXPR, type, expr);
  365.  
  366.   error ("aggregate value used where an integer was expected");
  367.  
  368.   {
  369.     register tree tem = build_int_2 (0, 0);
  370.     TREE_TYPE (tem) = type;
  371.     return tem;
  372.   }
  373. }
  374.  
  375. /* Create an expression whose value is that of EXPR,
  376.    converted to type TYPE.  The TREE_TYPE of the value
  377.    is always TYPE.  This function implements all reasonable
  378.    conversions; callers should filter out those that are
  379.    not permitted by the language being compiled.  */
  380.  
  381. tree
  382. convert (type, expr)
  383.      tree type, expr;
  384. {
  385.   register tree e = expr;
  386.   register enum tree_code code = TREE_CODE (type);
  387.  
  388.   if (type == TREE_TYPE (expr)
  389.       || TREE_CODE (expr) == ERROR_MARK)
  390.     return expr;
  391.   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
  392.     return fold (build1 (NOP_EXPR, type, expr));
  393.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  394.     return error_mark_node;
  395.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  396.     {
  397.       error ("void value not ignored as it ought to be");
  398.       return error_mark_node;
  399.     }
  400.   if (code == VOID_TYPE)
  401.     return build1 (CONVERT_EXPR, type, e);
  402. #if 0
  403.   /* This is incorrect.  A truncation can't be stripped this way.
  404.      Extensions will be stripped by the use of get_unwidened.  */
  405.   if (TREE_CODE (expr) == NOP_EXPR)
  406.     return convert (type, TREE_OPERAND (expr, 0));
  407. #endif
  408.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  409.     return fold (convert_to_integer (type, e));
  410.   if (code == POINTER_TYPE)
  411.     return fold (convert_to_pointer (type, e));
  412.   if (code == REAL_TYPE)
  413.     return fold (convert_to_real (type, e));
  414.  
  415.   error ("conversion to non-scalar type requested");
  416.   return error_mark_node;
  417. }
  418.