This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: TRUTH_*_EXPR and BOOLEAN_TYPE



Fergus --

It is definitely OK for a TRUTH_*_EXPR to have INTEGER_TYPE -- see for
example c-typeck.c

      if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
	   || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
	  && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
	      || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
	{
	  /* Result of these operations is always an int,
	     but that does not mean the operands should be
	     converted to ints!  */
	  result_type = integer_type_node;
	  op0 = truthvalue_conversion (op0);
	  op1 = truthvalue_conversion (op1);
	  converted = 1;
	}

However, it looks like what the C front-end does when you say `!x' for
`x' an integer variable is to build:

  TRUTH_NOT_EXPR (NE_EXPR (VAR_DECL, INTEGER_CST [0]))

with all the nodes having integer types.

The key constraint is not that the TRUTH_NOT_EXPR have BOOLEAN_TYPE --
it is that the value of the argument have value either zero or one.
(That's why the NE_EXPR is introduced.)  That's what tree.def is
trying to say with:

  Combination of boolean values or of integers considered only
  as zero or nonzero.

So, you should do that.  

I don't understand the assert in invert_truthvalue -- but if doing as
I suggest fixes the problems, then it won't really matter.

I checked in the attached patch to tree.def, in the hopes of
clarifying this point.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

2001-01-02  Mark Mitchell  <mark@codesourcery.com>

	* tree.def (TRUTH_NOT_EXPR): Improve documentation.

Index: tree.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.def,v
retrieving revision 1.33
retrieving revision 1.34
diff -c -p -r1.33 -r1.34
*** tree.def	2000/10/23 04:40:25	1.33
--- tree.def	2001/01/02 18:15:18	1.34
*************** DEFTREECODE (BIT_AND_EXPR, "bit_and_expr
*** 620,630 ****
  DEFTREECODE (BIT_ANDTC_EXPR, "bit_andtc_expr", '2', 2)
  DEFTREECODE (BIT_NOT_EXPR, "bit_not_expr", '1', 1)
  
! /* Combination of boolean values or of integers considered only
!    as zero or nonzero.  ANDIF and ORIF allow the second operand
!    not to be computed if the value of the expression is determined
!    from the first operand.  AND, OR, and XOR always compute the second
!    operand whether its value is needed or not (for side effects).  */
  DEFTREECODE (TRUTH_ANDIF_EXPR, "truth_andif_expr", 'e', 2)
  DEFTREECODE (TRUTH_ORIF_EXPR, "truth_orif_expr", 'e', 2)
  DEFTREECODE (TRUTH_AND_EXPR, "truth_and_expr", 'e', 2)
--- 620,634 ----
  DEFTREECODE (BIT_ANDTC_EXPR, "bit_andtc_expr", '2', 2)
  DEFTREECODE (BIT_NOT_EXPR, "bit_not_expr", '1', 1)
  
! /* ANDIF and ORIF allow the second operand not to be computed if the
!    value of the expression is determined from the first operand.  AND,
!    OR, and XOR always compute the second operand whether its value is
!    needed or not (for side effects).  The operand may have
!    BOOLEAN_TYPE or INTEGER_TYPE.  In either case, the argument will be
!    either zero or one.  For example, a TRUTH_NOT_EXPR will never have
!    a INTEGER_TYPE VAR_DECL as its argument; instead, a NE_EXPR will be
!    used to compare the VAR_DECL to zero, thereby obtaining a node with
!    value zero or one.  */
  DEFTREECODE (TRUTH_ANDIF_EXPR, "truth_andif_expr", 'e', 2)
  DEFTREECODE (TRUTH_ORIF_EXPR, "truth_orif_expr", 'e', 2)
  DEFTREECODE (TRUTH_AND_EXPR, "truth_and_expr", 'e', 2)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]