[M3devel] convert vs. cast in parse.c?

Tony Hosking hosking at cs.purdue.edu
Mon Sep 13 22:22:11 CEST 2010


m3_cast uses a NOP_EXPR, which assumes no code needs generating to effect the type change (i.e., bits are compatible, no extension necessary, etc.).
convert is provided to perform all reasonable conversions.  It generates code as necessary (see comments below from c-convert.c).
Looking at the comments for VIEW_CONVERT_EXPR, I wonder if we should be using that for LOOPHOLE on floats to avoid the need for the temporary?
I'm not sure we should ever be using CONVERT_EXPR explicitly — probably better to use convert and get the CONVERT_EXPR from that as necessary?



/* Change of width--truncation and extension of integers or reals--
   is represented with NOP_EXPR.  Proper functioning of many things
   assumes that no other conversions can be NOP_EXPRs.

   Conversion between integer and pointer is represented with CONVERT_EXPR.
   Converting integer to real uses FLOAT_EXPR
   and real to integer uses FIX_TRUNC_EXPR.

   Here is a list of all the functions that assume that widening and
   narrowing is always done with a NOP_EXPR:
     In convert.c, convert_to_integer.
     In c-typeck.c, build_binary_op (boolean ops), and
	c_common_truthvalue_conversion.
     In expr.c: expand_expr, for operands of a MULT_EXPR.
     In fold-const.c: fold.
     In tree.c: get_narrower and get_unwidened.  */
/* Subroutines of `convert'.  */

/* Create an expression whose value is that of EXPR,
   converted to type TYPE.  The TREE_TYPE of the value
   is always TYPE.  This function implements all reasonable
   conversions; callers should filter out those that are
   not permitted by the language being compiled.  */

tree
convert (tree type, tree expr)


Also:

NOP_EXPR
These nodes are used to represent conversions that do not require any code-generation. For example, conversion of a char* to an int* does not require any code be generated; such a conversion is represented by a NOP_EXPR. The single operand is the expression to be converted. The conversion from a pointer to a reference is also represented with a NOP_EXPR. 
CONVERT_EXPR
These nodes are similar to NOP_EXPRs, but are used in those situations where code may need to be generated. For example, if an int* is converted to an intcode may need to be generated on some platforms. These nodes are never used for C++-specific conversions, like conversions between pointers to different classes in an inheritance hierarchy. Any adjustments that need to be made in such cases are always indicated explicitly. Similarly, a user-defined conversion is never represented by a CONVERT_EXPR; instead, the function calls are made explicit. 
FIXED_CONVERT_EXPR
These nodes are used to represent conversions that involve fixed-point values. For example, from a fixed-point value to another fixed-point value, from an integer to a fixed-point value, from a fixed-point value to an integer, from a floating-point value to a fixed-point value, or from a fixed-point value to a floating-point value. 


Antony Hosking | Associate Professor | Computer Science | Purdue University
305 N. University Street | West Lafayette | IN 47907 | USA
Office +1 765 494 6001 | Mobile +1 765 427 5484




On 13 Sep 2010, at 06:31, Jay K wrote:

> 
> Tony, et. al. can you help me understand when in parse.c to use "convert" vs. "cast"?
> 
> Thanks,
>  - Jay
> 
> 
> 
> 
> 		 	   		  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20100913/4e046e3f/attachment-0002.html>


More information about the M3devel mailing list