2004-06-06 Randolph Chung <tausq@debian.org>
[deliverable/binutils-gdb.git] / gdb / valarith.c
index 5852ac6a8a155b1a7fe39f15492c157cfd910c38..1a86e921f42628a79e42583735a11e63f7f757a2 100644 (file)
@@ -1,7 +1,8 @@
 /* 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
-   Free Software Foundation, Inc.
+   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
+   Foundation, Inc.
 
    This file is part of GDB.
 
@@ -30,6 +31,7 @@
 #include "gdb_string.h"
 #include "doublest.h"
 #include <math.h>
+#include "infcall.h"
 
 /* Define whether or not the C operator '/' truncates towards zero for
    differently signed operands (truncation direction is undefined in C). */
@@ -43,24 +45,61 @@ static struct value *value_subscripted_rvalue (struct value *, struct value *, i
 void _initialize_valarith (void);
 \f
 
+/* Given a pointer, return the size of its target.
+   If the pointer type is void *, then return 1.
+   If the target type is incomplete, then error out.
+   This isn't a general purpose function, but just a 
+   helper for value_sub & value_add.
+*/
+
+static LONGEST
+find_size_for_pointer_math (struct type *ptr_type)
+{
+  LONGEST sz = -1;
+  struct type *ptr_target;
+
+  ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
+
+  sz = TYPE_LENGTH (ptr_target);
+  if (sz == 0)
+    {
+      if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
+       sz = 1;
+      else
+       {
+         char *name;
+         
+         name = TYPE_NAME (ptr_target);
+         if (name == NULL)
+           name = TYPE_TAG_NAME (ptr_target);
+         if (name == NULL)
+           error ("Cannot perform pointer math on incomplete types, "
+                  "try casting to a known type, or void *.");
+         else
+           error ("Cannot perform pointer math on incomplete type \"%s\", "
+                  "try casting to a known type, or void *.", name);
+       }
+    }
+  return sz;
+}
+
 struct value *
 value_add (struct value *arg1, struct value *arg2)
 {
   struct value *valint;
   struct value *valptr;
-  register int len;
+  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;
@@ -77,12 +116,12 @@ value_add (struct value *arg1, struct value *arg2)
          valint = arg1;
          valptrtype = type2;
        }
-      len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
-      if (len == 0)
-       len = 1;                /* For (void *) */
+
+      sz = find_size_for_pointer_math (valptrtype);
+
       retval = value_from_pointer (valptrtype,
                                   value_as_address (valptr)
-                                  + (len * value_as_long (valint)));
+                                  + (sz * value_as_long (valint)));
       VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
       return retval;
     }
@@ -94,17 +133,18 @@ 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 = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
+         LONGEST sz = find_size_for_pointer_math (type1);
+
          return value_from_pointer (type1,
                                     (value_as_address (arg1)
                                      - (sz * value_as_long (arg2))));
@@ -236,6 +276,7 @@ value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound
   else
     VALUE_LVAL (v) = VALUE_LVAL (array);
   VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
+  VALUE_REGNO (v) = VALUE_REGNO (array);
   VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
   return v;
 }
@@ -710,22 +751,12 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
 
   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
@@ -759,7 +790,7 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
         case BINOP_EXP:
           v = pow (v1, v2);
           if (errno)
-            error ("Cannot perform exponentiation: %s", strerror (errno));
+            error ("Cannot perform exponentiation: %s", safe_strerror (errno));
           break;
 
        default:
@@ -820,7 +851,7 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
     /* Integral operations here.  */
     /* FIXME:  Also mixed integral/booleans, with result an integer. */
     /* FIXME: This implements ANSI C rules (also correct for C++).
-       What about FORTRAN and chill?  */
+       What about FORTRAN and (the deleted) chill ?  */
     {
       unsigned int promoted_len1 = TYPE_LENGTH (type1);
       unsigned int promoted_len2 = TYPE_LENGTH (type2);
@@ -897,7 +928,7 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
             case BINOP_EXP:
               v = pow (v1, v2);
               if (errno)
-                error ("Cannot perform exponentiation: %s", strerror (errno));
+                error ("Cannot perform exponentiation: %s", safe_strerror (errno));
               break;
 
            case BINOP_REM:
@@ -907,12 +938,6 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
            case BINOP_MOD:
              /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
                 v1 mod 0 has a defined value, v1. */
-             /* Chill specifies that v2 must be > 0, so check for that. */
-             if (current_language->la_language == language_chill
-                 && value_as_long (arg2) <= 0)
-               {
-                 error ("Second operand of MOD must be greater than zero.");
-               }
              if (v2 == 0)
                {
                  v = v1;
@@ -1021,7 +1046,7 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
             case BINOP_EXP:
               v = pow (v1, v2);
               if (errno)
-                error ("Cannot perform exponentiation: %s", strerror (errno));
+                error ("Cannot perform exponentiation: %s", safe_strerror (errno));
              break;
 
            case BINOP_REM:
@@ -1031,12 +1056,6 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
            case BINOP_MOD:
              /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
                 X mod 0 has a defined value, X. */
-             /* Chill specifies that v2 must be > 0, so check for that. */
-             if (current_language->la_language == language_chill
-                 && v2 <= 0)
-               {
-                 error ("Second operand of MOD must be greater than zero.");
-               }
              if (v2 == 0)
                {
                  v = v1;
@@ -1128,8 +1147,8 @@ value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
 int
 value_logical_not (struct value *arg1)
 {
-  register int len;
-  register char *p;
+  int len;
+  char *p;
   struct type *type1;
 
   COERCE_NUMBER (arg1);
@@ -1186,33 +1205,35 @@ value_strcmp (struct value *arg1, struct value *arg2)
 int
 value_equal (struct value *arg1, struct value *arg2)
 {
-  register int len;
-  register char *p1, *p2;
+  int len;
+  char *p1, *p2;
   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
@@ -1245,33 +1266,35 @@ value_equal (struct value *arg1, struct value *arg2)
 int
 value_less (struct value *arg1, struct value *arg2)
 {
-  register enum type_code code1;
-  register enum type_code code2;
+  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;
@@ -1287,20 +1310,19 @@ value_less (struct value *arg1, struct value *arg2)
 struct value *
 value_neg (struct value *arg1)
 {
-  register struct type *type;
-  register struct type *result_type = VALUE_TYPE (arg1);
+  struct type *type;
+  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 chill ?  */
+      /* Perform integral promotion for ANSI C/C++.  FIXME: What about
+         FORTRAN and (the deleted) chill ?  */
       if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
        result_type = builtin_type_int;
 
@@ -1316,17 +1338,14 @@ value_neg (struct value *arg1)
 struct value *
 value_complement (struct value *arg1)
 {
-  register struct type *type;
-  register struct type *result_type = VALUE_TYPE (arg1);
-  int typecode;
+  struct type *type;
+  struct type *result_type = VALUE_TYPE (arg1);
 
   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++.
This page took 0.029825 seconds and 4 git commands to generate.