*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / valarith.c
index 7c553d1ca4a2db618b5932dafa6ad6ac43a9ac2a..68f649f3c078ce13820d4293f21e724189675f5b 100644 (file)
@@ -2,7 +2,7 @@
 
    Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
    1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
-   2010 Free Software Foundation, Inc.
+   2010, 2011 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -34,7 +34,7 @@
 #include "exceptions.h"
 
 /* Define whether or not the C operator '/' truncates towards zero for
-   differently signed operands (truncation direction is undefined in C). */
+   differently signed operands (truncation direction is undefined in C).  */
 
 #ifndef TRUNCATION_TOWARDS_ZERO
 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
@@ -47,8 +47,7 @@ void _initialize_valarith (void);
    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_ptradd.
-*/
+   helper for value_ptradd.  */
 
 static LONGEST
 find_size_for_pointer_math (struct type *ptr_type)
@@ -90,13 +89,17 @@ value_ptradd (struct value *arg1, LONGEST arg2)
 {
   struct type *valptrtype;
   LONGEST sz;
+  struct value *result;
 
   arg1 = coerce_array (arg1);
   valptrtype = check_typedef (value_type (arg1));
   sz = find_size_for_pointer_math (valptrtype);
 
-  return value_from_pointer (valptrtype,
-                            value_as_address (arg1) + sz * arg2);
+  result = value_from_pointer (valptrtype,
+                              value_as_address (arg1) + sz * arg2);
+  if (VALUE_LVAL (result) != lval_internalvar)
+    set_value_component_location (result, arg1);
+  return result;
 }
 
 /* Given two compatible pointer values ARG1 and ARG2, return the
@@ -118,9 +121,9 @@ value_ptrdiff (struct value *arg1, struct value *arg2)
 
   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
-    error (_("\
-First argument of `-' is a pointer and second argument is neither\n\
-an integer nor a pointer of the same type."));
+    error (_("First argument of `-' is a pointer and "
+            "second argument is neither\n"
+            "an integer nor a pointer of the same type."));
 
   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
   if (sz == 0) 
@@ -142,7 +145,7 @@ an integer nor a pointer of the same type."));
    See comments in value_coerce_array() for rationale for reason for
    doing lower bounds adjustment here rather than there.
    FIXME:  Perhaps we should validate that the index is valid and if
-   verbosity is set, warn about invalid indices (but still use them). */
+   verbosity is set, warn about invalid indices (but still use them).  */
 
 struct value *
 value_subscript (struct value *array, LONGEST index)
@@ -202,12 +205,15 @@ value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
                             && elt_offs >= TYPE_LENGTH (array_type)))
     error (_("no such vector element"));
 
-  v = allocate_value (elt_type);
   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
-    set_value_lazy (v, 1);
+    v = allocate_value_lazy (elt_type);
   else
-    memcpy (value_contents_writeable (v),
-           value_contents (array) + elt_offs, elt_size);
+    {
+      v = allocate_value (elt_type);
+      value_contents_copy (v, value_embedded_offset (v),
+                          array, value_embedded_offset (array) + elt_offs,
+                          elt_size);
+    }
 
   set_value_component_location (v, array);
   VALUE_REGNUM (v) = VALUE_REGNUM (array);
@@ -309,15 +315,9 @@ unop_user_defined_p (enum exp_opcode op, struct value *arg1)
   if (op == UNOP_ADDR)
     return 0;
   type1 = check_typedef (value_type (arg1));
-  for (;;)
-    {
-      if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
-       return 1;
-      else if (TYPE_CODE (type1) == TYPE_CODE_REF)
-       type1 = TYPE_TARGET_TYPE (type1);
-      else
-       return 0;
-    }
+  if (TYPE_CODE (type1) == TYPE_CODE_REF)
+    type1 = check_typedef (TYPE_TARGET_TYPE (type1));
+  return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
 }
 
 /* Try to find an operator named OPERATOR which takes NARGS arguments
@@ -339,7 +339,7 @@ value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
   int i;
 
   arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
-  /* Prepare list of argument types for overload resolution */
+  /* Prepare list of argument types for overload resolution */
   for (i = 0; i < nargs; i++)
     arg_types[i] = value_type (args[i]);
 
@@ -413,7 +413,7 @@ value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
   argvec[2] = arg2;
   argvec[3] = 0;
 
-  /* make the right function name up */
+  /* Make the right function name up.  */
   strcpy (tstr, "operator__");
   ptr = tstr + 8;
   switch (op)
@@ -539,7 +539,8 @@ value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
            = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
          return value_zero (return_type, VALUE_LVAL (arg1));
        }
-      return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
+      return call_function_by_hand (argvec[0], 2 - static_memfuncp,
+                                   argvec + 1);
     }
   throw_error (NOT_FOUND_ERROR,
                _("member function %s not found"), tstr);
@@ -549,7 +550,7 @@ value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
 }
 
 /* We know that arg1 is a structure, so try to find a unary user
-   defined operator that matches the operator in question.  
+   defined operator that matches the operator in question.
    Create an argument vector that calls arg1.operator @ (arg1)
    and return that value (where '@' is (almost) any unary operator which
    is legal for GNU C++).  */
@@ -577,7 +578,7 @@ value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
 
   nargs = 1;
 
-  /* make the right function name up */
+  /* Make the right function name up.  */
   strcpy (tstr, "operator__");
   ptr = tstr + 8;
   strcpy (mangle_tstr, "__");
@@ -670,8 +671,7 @@ value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
    values of length 1.
 
    (3)  Character values are also allowed and are treated as character
-   string values of length 1.
- */
+   string values of length 1.  */
 
 struct value *
 value_concat (struct value *arg1, struct value *arg2)
@@ -691,7 +691,7 @@ value_concat (struct value *arg1, struct value *arg2)
      or a repeat count and a value to be repeated.  INVAL1 is set to the
      first of two concatenated values, or the repeat count.  INVAL2 is set
      to the second of the two concatenated values or the value to be 
-     repeated. */
+     repeated.  */
 
   if (TYPE_CODE (type2) == TYPE_CODE_INT)
     {
@@ -708,12 +708,12 @@ value_concat (struct value *arg1, struct value *arg2)
       inval2 = arg2;
     }
 
-  /* Now process the input values. */
+  /* Now process the input values.  */
 
   if (TYPE_CODE (type1) == TYPE_CODE_INT)
     {
       /* We have a repeat count.  Validate the second value and then
-         construct a value repeated that many times. */
+         construct a value repeated that many times.  */
       if (TYPE_CODE (type2) == TYPE_CODE_STRING
          || TYPE_CODE (type2) == TYPE_CODE_CHAR)
        {
@@ -756,7 +756,7 @@ value_concat (struct value *arg1, struct value *arg2)
   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
           || TYPE_CODE (type1) == TYPE_CODE_CHAR)
     {
-      /* We have two character strings to concatenate. */
+      /* We have two character strings to concatenate.  */
       if (TYPE_CODE (type2) != TYPE_CODE_STRING
          && TYPE_CODE (type2) != TYPE_CODE_CHAR)
        {
@@ -791,17 +791,18 @@ value_concat (struct value *arg1, struct value *arg2)
   else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
           || TYPE_CODE (type1) == TYPE_CODE_BOOL)
     {
-      /* We have two bitstrings to concatenate. */
+      /* We have two bitstrings to concatenate.  */
       if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
          && TYPE_CODE (type2) != TYPE_CODE_BOOL)
        {
-         error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
+         error (_("Bitstrings or booleans can only be concatenated "
+                  "with other bitstrings or booleans."));
        }
       error (_("unimplemented support for bitstring/boolean concatenation."));
     }
   else
     {
-      /* We don't know how to concatenate these operands. */
+      /* We don't know how to concatenate these operands.  */
       error (_("illegal operands for concatenation."));
     }
   return (outval);
@@ -809,6 +810,7 @@ value_concat (struct value *arg1, struct value *arg2)
 \f
 /* Integer exponentiation: V1**V2, where both arguments are
    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
+
 static LONGEST
 integer_pow (LONGEST v1, LONGEST v2)
 {
@@ -821,7 +823,7 @@ integer_pow (LONGEST v1, LONGEST v2)
     }
   else 
     {
-      /* The Russian Peasant's Algorithm */
+      /* The Russian Peasant's Algorithm */
       LONGEST v;
       
       v = 1;
@@ -839,6 +841,7 @@ integer_pow (LONGEST v1, LONGEST v2)
 
 /* Integer exponentiation: V1**V2, where both arguments are
    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
+
 static ULONGEST
 uinteger_pow (ULONGEST v1, LONGEST v2)
 {
@@ -851,7 +854,7 @@ uinteger_pow (ULONGEST v1, LONGEST v2)
     }
   else 
     {
-      /* The Russian Peasant's Algorithm */
+      /* The Russian Peasant's Algorithm */
       ULONGEST v;
       
       v = 1;
@@ -888,7 +891,8 @@ value_args_as_decimal (struct value *arg1, struct value *arg2,
     /* The DFP extension to the C language does not allow mixing of
      * decimal float types with other float types in expressions
      * (see WDTR 24732, page 12).  */
-    error (_("Mixing decimal floating types with other floating types is not allowed."));
+    error (_("Mixing decimal floating types with "
+            "other floating types is not allowed."));
 
   /* Obtain decimal value of arg1, converting from other types
      if necessary.  */
@@ -1031,7 +1035,8 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
          errno = 0;
          v = pow (v1, v2);
          if (errno)
-           error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
+           error (_("Cannot perform exponentiation: %s"),
+                  safe_strerror (errno));
          break;
 
        case BINOP_MIN:
@@ -1167,7 +1172,7 @@ scalar_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. */
+                v1 mod 0 has a defined value, v1.  */
              if (v2 == 0)
                {
                  v = v1;
@@ -1175,7 +1180,7 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
              else
                {
                  v = v1 / v2;
-                 /* Note floor(v1/v2) == v1/v2 for unsigned. */
+                 /* Note floor(v1/v2) == v1/v2 for unsigned.  */
                  v = v1 - (v2 * v);
                }
              break;
@@ -1293,7 +1298,7 @@ scalar_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. */
+                X mod 0 has a defined value, X.  */
              if (v2 == 0)
                {
                  v = v1;
@@ -1301,7 +1306,7 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
              else
                {
                  v = v1 / v2;
-                 /* Compute floor. */
+                 /* Compute floor.  */
                  if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
                    {
                      v--;
@@ -1503,7 +1508,7 @@ value_logical_not (struct value *arg1)
 }
 
 /* Perform a comparison on two string values (whose content are not
-   necessarily null terminated) based on their length */
+   necessarily null terminated) based on their length */
 
 static int
 value_strcmp (struct value *arg1, struct value *arg2)
@@ -1609,7 +1614,7 @@ value_equal (struct value *arg1, struct value *arg2)
   else
     {
       error (_("Invalid type combination in equality test."));
-      return 0;                        /* For lint -- never reached */
+      return 0;                        /* For lint -- never reached */
     }
 }
 
@@ -1723,8 +1728,8 @@ value_pos (struct value *arg1)
     }
   else
     {
-      error ("Argument to positive operation not a number.");
-      return 0;                        /* For lint -- never reached */
+      error (_("Argument to positive operation not a number."));
+      return 0;                        /* For lint -- never reached */
     }
 }
 
@@ -1740,7 +1745,7 @@ value_neg (struct value *arg1)
     {
       struct value *val = allocate_value (type);
       int len = TYPE_LENGTH (type);
-      gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long */
+      gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long */
 
       memcpy (decbytes, value_contents (arg1), len);
 
@@ -1762,9 +1767,13 @@ value_neg (struct value *arg1)
     {
       struct value *tmp, *val = allocate_value (type);
       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
-      int i, n = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
+      int i;
+      LONGEST low_bound, high_bound;
+
+      if (!get_array_bounds (type, &low_bound, &high_bound))
+       error (_("Could not determine the vector bounds"));
 
-      for (i = 0; i < n; i++)
+      for (i = 0; i < high_bound - low_bound + 1; i++)
        {
          tmp = value_neg (value_subscript (arg1, i));
          memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
@@ -1775,7 +1784,7 @@ value_neg (struct value *arg1)
   else
     {
       error (_("Argument to negate operation not a number."));
-      return 0;                        /* For lint -- never reached */
+      return 0;                        /* For lint -- never reached */
     }
 }
 
@@ -1794,10 +1803,14 @@ value_complement (struct value *arg1)
     {
       struct value *tmp;
       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
-      int i, n = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
+      int i;
+      LONGEST low_bound, high_bound;
+
+      if (!get_array_bounds (type, &low_bound, &high_bound))
+       error (_("Could not determine the vector bounds"));
 
       val = allocate_value (type);
-      for (i = 0; i < n; i++)
+      for (i = 0; i < high_bound - low_bound + 1; i++)
         {
           tmp = value_complement (value_subscript (arg1, i));
           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
@@ -1812,7 +1825,7 @@ value_complement (struct value *arg1)
 \f
 /* The INDEX'th bit of SET value whose value_type is TYPE,
    and whose value_contents is valaddr.
-   Return -1 if out of range, -2 other error. */
+   Return -1 if out of range, -2 other error.  */
 
 int
 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
This page took 0.046987 seconds and 4 git commands to generate.