This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA]: Extend some arithmetic operations to range types.


Here is a second draft of the patch I submitted on 26 March.  I
believe I have addressed Daniel's comments.  Rather than using a
'base_type' function to strip off range-type wrappers, I use the
existing is_integral_type.  This has the side-effect of extending some
of the operations to work on BOOL or CHAR, which I am assuming is
harmless.  By using is_integral_type, we also can eliminate some calls
to COERCE_ENUM.  For consistency and to tidy things up a bit, I have
taken the liberty of changing value_binop to also use is_integral_type.

Comments?

Paul Hilfinger
ACT, Inc.

2004-03-26  Paul N. Hilfinger  <Hilfinger@gnat.com>

	* valarith.c: Update copyright notice.
	(value_add): Handle range types.
	(value_sub): Ditto.
	(value_equal): Ditto.
	(value_less): Ditto.
	(value_neg): Ditto.
	(value_complement): Ditto.
	(value_binop): Simplify slightly by using is_integral_type and 
	eliminiating unnecessary COERCE_ENUMs.
	
Index: current-public.55/gdb/valarith.c
--- current-public.55/gdb/valarith.c Wed, 17 Sep 2003 23:22:28 -0700 hilfingr (GdbPub/l/16_valarith.c 1.1.1.3 644)
+++ submit.50(w)/gdb/valarith.c Sun, 28 Mar 2004 01:42:22 -0800 hilfingr (GdbPub/l/16_valarith.c 1.1.1.3.2.3 644)
@@ -1,7 +1,7 @@
 /* Perform arithmetic and other operations on values, for GDB.
 
    Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
-   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
+   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
    Foundation, Inc.
 
    This file is part of GDB.
@@ -91,16 +91,15 @@ value_add (struct value *arg1, struct va
   LONGEST sz;
   struct type *type1, *type2, *valptrtype;
 
-  COERCE_NUMBER (arg1);
-  COERCE_NUMBER (arg2);
+  COERCE_ARRAY (arg1);
+  COERCE_ARRAY (arg2);
   type1 = check_typedef (VALUE_TYPE (arg1));
   type2 = check_typedef (VALUE_TYPE (arg2));
 
   if ((TYPE_CODE (type1) == TYPE_CODE_PTR
        || TYPE_CODE (type2) == TYPE_CODE_PTR)
       &&
-      (TYPE_CODE (type1) == TYPE_CODE_INT
-       || TYPE_CODE (type2) == TYPE_CODE_INT))
+      (is_integral_type (type1) || is_integral_type (type2)))
     /* Exactly one argument is a pointer, and one is an integer.  */
     {
       struct value *retval;
@@ -134,14 +133,14 @@ struct value *
 value_sub (struct value *arg1, struct value *arg2)
 {
   struct type *type1, *type2;
-  COERCE_NUMBER (arg1);
-  COERCE_NUMBER (arg2);
+  COERCE_ARRAY (arg1);
+  COERCE_ARRAY (arg2);
   type1 = check_typedef (VALUE_TYPE (arg1));
   type2 = check_typedef (VALUE_TYPE (arg2));
 
   if (TYPE_CODE (type1) == TYPE_CODE_PTR)
     {
-      if (TYPE_CODE (type2) == TYPE_CODE_INT)
+      if (is_integral_type (type2))
 	{
 	  /* pointer - integer.  */
 	  LONGEST sz = find_size_for_pointer_math (type1);
@@ -752,22 +751,12 @@ value_binop (struct value *arg1, struct 
 
   COERCE_REF (arg1);
   COERCE_REF (arg2);
-  COERCE_ENUM (arg1);
-  COERCE_ENUM (arg2);
   type1 = check_typedef (VALUE_TYPE (arg1));
   type2 = check_typedef (VALUE_TYPE (arg2));
 
-  if ((TYPE_CODE (type1) != TYPE_CODE_FLT
-       && TYPE_CODE (type1) != TYPE_CODE_CHAR
-       && TYPE_CODE (type1) != TYPE_CODE_INT
-       && TYPE_CODE (type1) != TYPE_CODE_BOOL
-       && TYPE_CODE (type1) != TYPE_CODE_RANGE)
+  if ((TYPE_CODE (type1) != TYPE_CODE_FLT && !is_integral_type (type1))
       ||
-      (TYPE_CODE (type2) != TYPE_CODE_FLT
-       && TYPE_CODE (type2) != TYPE_CODE_CHAR
-       && TYPE_CODE (type2) != TYPE_CODE_INT
-       && TYPE_CODE (type2) != TYPE_CODE_BOOL
-       && TYPE_CODE (type2) != TYPE_CODE_RANGE))
+      (TYPE_CODE (type2) != TYPE_CODE_FLT && !is_integral_type (type2)))
     error ("Argument to arithmetic operation not a number or boolean.");
 
   if (TYPE_CODE (type1) == TYPE_CODE_FLT
@@ -1221,28 +1210,30 @@ value_equal (struct value *arg1, struct 
   struct type *type1, *type2;
   enum type_code code1;
   enum type_code code2;
+  int is_int1, is_int2;
 
-  COERCE_NUMBER (arg1);
-  COERCE_NUMBER (arg2);
+  COERCE_ARRAY (arg1);
+  COERCE_ARRAY (arg2);
 
   type1 = check_typedef (VALUE_TYPE (arg1));
   type2 = check_typedef (VALUE_TYPE (arg2));
   code1 = TYPE_CODE (type1);
   code2 = TYPE_CODE (type2);
+  is_int1 = is_integral_type (type1);
+  is_int2 = is_integral_type (type2);
 
-  if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
-      (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  if (is_int1 && is_int2)
     return longest_to_int (value_as_long (value_binop (arg1, arg2,
 						       BINOP_EQUAL)));
-  else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
-	   && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  else if ((code1 == TYPE_CODE_FLT || is_int1)
+	   && (code2 == TYPE_CODE_FLT || is_int2))
     return value_as_double (arg1) == value_as_double (arg2);
 
   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
      is bigger.  */
-  else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  else if (code1 == TYPE_CODE_PTR && is_int2)
     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
-  else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
+  else if (code2 == TYPE_CODE_PTR && is_int1)
     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
 
   else if (code1 == code2
@@ -1278,30 +1269,32 @@ value_less (struct value *arg1, struct v
   enum type_code code1;
   enum type_code code2;
   struct type *type1, *type2;
+  int is_int1, is_int2;
 
-  COERCE_NUMBER (arg1);
-  COERCE_NUMBER (arg2);
+  COERCE_ARRAY (arg1);
+  COERCE_ARRAY (arg2);
 
   type1 = check_typedef (VALUE_TYPE (arg1));
   type2 = check_typedef (VALUE_TYPE (arg2));
   code1 = TYPE_CODE (type1);
   code2 = TYPE_CODE (type2);
+  is_int1 = is_integral_type (type1);
+  is_int2 = is_integral_type (type2);
 
-  if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
-      (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  if (is_int1 && is_int2)
     return longest_to_int (value_as_long (value_binop (arg1, arg2,
 						       BINOP_LESS)));
-  else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
-	   && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  else if ((code1 == TYPE_CODE_FLT || is_int1)
+	   && (code2 == TYPE_CODE_FLT || is_int2))
     return value_as_double (arg1) < value_as_double (arg2);
   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
     return value_as_address (arg1) < value_as_address (arg2);
 
   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
      is bigger.  */
-  else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
+  else if (code1 == TYPE_CODE_PTR && is_int2)
     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
-  else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
+  else if (code2 == TYPE_CODE_PTR && is_int1)
     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
     return value_strcmp (arg1, arg2) < 0;
@@ -1321,13 +1314,12 @@ value_neg (struct value *arg1)
   struct type *result_type = VALUE_TYPE (arg1);
 
   COERCE_REF (arg1);
-  COERCE_ENUM (arg1);
 
   type = check_typedef (VALUE_TYPE (arg1));
 
   if (TYPE_CODE (type) == TYPE_CODE_FLT)
     return value_from_double (result_type, -value_as_double (arg1));
-  else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
+  else if (is_integral_type (type))
     {
       /* Perform integral promotion for ANSI C/C++.  FIXME: What about
          FORTRAN and (the deleted) chill ?  */
@@ -1348,15 +1340,12 @@ value_complement (struct value *arg1)
 {
   struct type *type;
   struct type *result_type = VALUE_TYPE (arg1);
-  int typecode;
 
   COERCE_REF (arg1);
-  COERCE_ENUM (arg1);
 
   type = check_typedef (VALUE_TYPE (arg1));
 
-  typecode = TYPE_CODE (type);
-  if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
+  if (!is_integral_type (type))
     error ("Argument to complement operation not an integer or boolean.");
 
   /* Perform integral promotion for ANSI C/C++.


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