gdb/doc:
[deliverable/binutils-gdb.git] / gdb / valarith.c
index f6e3a0572429ee819b3421fd7473d8b802fa6452..c457f4a63095411ee092355c73afaa5e44cc8dae 100644 (file)
@@ -1,8 +1,7 @@
 /* Perform arithmetic and other operations on values, for GDB.
 
-   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.
+   Copyright (C) 1986, 1988-2005, 2007-2012 Free Software Foundation,
+   Inc.
 
    This file is part of GDB.
 
@@ -34,7 +33,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 +46,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)
@@ -66,7 +64,7 @@ find_size_for_pointer_math (struct type *ptr_type)
        sz = 1;
       else
        {
-         char *name;
+         const char *name;
          
          name = TYPE_NAME (ptr_target);
          if (name == NULL)
@@ -90,13 +88,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 +120,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) 
@@ -137,12 +139,11 @@ an integer nor a pointer of the same type."));
 
    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
    current language supports C-style arrays, it may also be TYPE_CODE_PTR.
-   To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
 
    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 +203,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);
@@ -216,46 +220,6 @@ value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
   return v;
 }
 
-/* Return the value of BITSTRING[IDX] as (boolean) type TYPE.  */
-
-struct value *
-value_bitstring_subscript (struct type *type,
-                          struct value *bitstring, LONGEST index)
-{
-
-  struct type *bitstring_type, *range_type;
-  struct value *v;
-  int offset, byte, bit_index;
-  LONGEST lowerbound, upperbound;
-
-  bitstring_type = check_typedef (value_type (bitstring));
-  gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
-
-  range_type = TYPE_INDEX_TYPE (bitstring_type);
-  get_discrete_bounds (range_type, &lowerbound, &upperbound);
-  if (index < lowerbound || index > upperbound)
-    error (_("bitstring index out of range"));
-
-  index -= lowerbound;
-  offset = index / TARGET_CHAR_BIT;
-  byte = *((char *) value_contents (bitstring) + offset);
-
-  bit_index = index % TARGET_CHAR_BIT;
-  byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
-           TARGET_CHAR_BIT - 1 - bit_index : bit_index);
-
-  v = value_from_longest (type, byte & 1);
-
-  set_value_bitpos (v, bit_index);
-  set_value_bitsize (v, 1);
-  set_value_component_location (v, bitstring);
-  VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
-
-  set_value_offset (v, offset + value_offset (bitstring));
-
-  return v;
-}
-
 \f
 /* Check to see if either argument is a structure, or a reference to
    one.  This is called so we know whether to go ahead with the normal
@@ -274,7 +238,7 @@ binop_types_user_defined_p (enum exp_opcode op,
   if (TYPE_CODE (type1) == TYPE_CODE_REF)
     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
 
-  type2 = check_typedef (type1);
+  type2 = check_typedef (type2);
   if (TYPE_CODE (type2) == TYPE_CODE_REF)
     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
 
@@ -309,15 +273,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
@@ -335,15 +293,8 @@ value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
 
   struct symbol *symp = NULL;
   struct value *valp = NULL;
-  struct type **arg_types;
-  int i;
-
-  arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
-  /* Prepare list of argument types for overload resolution */
-  for (i = 0; i < nargs; i++)
-    arg_types[i] = value_type (args[i]);
 
-  find_overload_match (arg_types, nargs, operator, BOTH /* could be method */,
+  find_overload_match (args, nargs, operator, BOTH /* could be method */,
                        0 /* strict match */, &args[0], /* objp */
                        NULL /* pass NULL symbol since symbol is unknown */,
                        &valp, &symp, static_memfuncp, 0);
@@ -413,7 +364,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 +490,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 +501,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 +529,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 +622,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 +642,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,18 +659,21 @@ 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)
        {
+         struct cleanup *back_to;
+
          count = longest_to_int (value_as_long (inval1));
          inval2len = TYPE_LENGTH (type2);
-         ptr = (char *) alloca (count * inval2len);
+         ptr = (char *) xmalloc (count * inval2len);
+         back_to = make_cleanup (xfree, ptr);
          if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
            {
              char_type = type2;
@@ -742,11 +696,11 @@ value_concat (struct value *arg1, struct value *arg2)
                }
            }
          outval = value_string (ptr, count * inval2len, char_type);
+         do_cleanups (back_to);
        }
-      else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
-              || TYPE_CODE (type2) == TYPE_CODE_BOOL)
+      else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
        {
-         error (_("unimplemented support for bitstring/boolean repeats"));
+         error (_("unimplemented support for boolean repeats"));
        }
       else
        {
@@ -756,7 +710,9 @@ 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. */
+      struct cleanup *back_to;
+
+      /* We have two character strings to concatenate.  */
       if (TYPE_CODE (type2) != TYPE_CODE_STRING
          && TYPE_CODE (type2) != TYPE_CODE_CHAR)
        {
@@ -764,7 +720,8 @@ value_concat (struct value *arg1, struct value *arg2)
        }
       inval1len = TYPE_LENGTH (type1);
       inval2len = TYPE_LENGTH (type2);
-      ptr = (char *) alloca (inval1len + inval2len);
+      ptr = (char *) xmalloc (inval1len + inval2len);
+      back_to = make_cleanup (xfree, ptr);
       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
        {
          char_type = type1;
@@ -787,21 +744,21 @@ value_concat (struct value *arg1, struct value *arg2)
          memcpy (ptr + inval1len, value_contents (inval2), inval2len);
        }
       outval = value_string (ptr, inval1len + inval2len, char_type);
+      do_cleanups (back_to);
     }
-  else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
-          || TYPE_CODE (type1) == TYPE_CODE_BOOL)
+  else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
     {
-      /* We have two bitstrings to concatenate. */
-      if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
-         && TYPE_CODE (type2) != TYPE_CODE_BOOL)
+      /* We have two bitstrings to concatenate.  */
+      if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
        {
-         error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
+         error (_("Booleans can only be concatenated "
+                  "with other bitstrings or booleans."));
        }
-      error (_("unimplemented support for bitstring/boolean concatenation."));
+      error (_("unimplemented support for 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 +766,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 +779,7 @@ integer_pow (LONGEST v1, LONGEST v2)
     }
   else 
     {
-      /* The Russian Peasant's Algorithm */
+      /* The Russian Peasant's Algorithm */
       LONGEST v;
       
       v = 1;
@@ -839,6 +797,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 +810,7 @@ uinteger_pow (ULONGEST v1, LONGEST v2)
     }
   else 
     {
-      /* The Russian Peasant's Algorithm */
+      /* The Russian Peasant's Algorithm */
       ULONGEST v;
       
       v = 1;
@@ -888,7 +847,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 +991,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 +1128,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 +1136,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 +1254,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 +1262,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--;
@@ -1393,8 +1354,9 @@ static struct value *
 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
 {
   struct value *val, *tmp, *mark;
-  struct type *type1, *type2, *eltype1, *eltype2, *result_type;
-  int t1_is_vec, t2_is_vec, elsize, n, i;
+  struct type *type1, *type2, *eltype1, *eltype2;
+  int t1_is_vec, t2_is_vec, elsize, i;
+  LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
 
   type1 = check_typedef (value_type (val1));
   type2 = check_typedef (value_type (val2));
@@ -1407,23 +1369,23 @@ vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
   if (!t1_is_vec || !t2_is_vec)
     error (_("Vector operations are only supported among vectors"));
 
+  if (!get_array_bounds (type1, &low_bound1, &high_bound1)
+      || !get_array_bounds (type2, &low_bound2, &high_bound2))
+    error (_("Could not determine the vector bounds"));
+
   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
+  elsize = TYPE_LENGTH (eltype1);
 
   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
-      || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
-      || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2))
+      || elsize != TYPE_LENGTH (eltype2)
+      || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
+      || low_bound1 != low_bound2 || high_bound1 != high_bound2)
     error (_("Cannot perform operation on vectors with different types"));
 
-  elsize = TYPE_LENGTH (eltype1);
-  n = TYPE_LENGTH (type1) / elsize;
-
-  if (n != TYPE_LENGTH (type2) / TYPE_LENGTH (eltype2))
-    error (_("Cannot perform operation on vectors with different sizes"));
-
   val = allocate_value (type1);
   mark = value_mark ();
-  for (i = 0; i < n; i++)
+  for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
     {
       tmp = value_binop (value_subscript (val1, i),
                         value_subscript (val2, i), op);
@@ -1502,7 +1464,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)
@@ -1608,7 +1570,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 */
     }
 }
 
@@ -1722,8 +1684,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 */
     }
 }
 
@@ -1739,7 +1701,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);
 
@@ -1761,9 +1723,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;
 
-      for (i = 0; i < n; i++)
+      if (!get_array_bounds (type, &low_bound, &high_bound))
+       error (_("Could not determine the vector bounds"));
+
+      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),
@@ -1774,7 +1740,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 */
     }
 }
 
@@ -1793,10 +1759,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),
@@ -1811,7 +1781,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.032425 seconds and 4 git commands to generate.