gdb: set only inferior_ptid in sparc_{fetch,store}_inferior_registers
[binutils-gdb.git] / gdb / valops.c
index 09e97d2d3b9797f8b587aea7f12ac653d26f2028..8694c124b5248a468d8debdfa94d594dee1911c5 100644 (file)
@@ -1,6 +1,6 @@
 /* Perform non-arithmetic operations on values, for GDB.
 
-   Copyright (C) 1986-2020 Free Software Foundation, Inc.
+   Copyright (C) 1986-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -331,6 +331,39 @@ value_cast_pointers (struct type *type, struct value *arg2,
   return arg2;
 }
 
+/* See value.h.  */
+
+gdb_mpq
+value_to_gdb_mpq (struct value *value)
+{
+  struct type *type = check_typedef (value_type (value));
+
+  gdb_mpq result;
+  if (is_floating_type (type))
+    {
+      double d = target_float_to_host_double (value_contents (value),
+                                             type);
+      mpq_set_d (result.val, d);
+    }
+  else
+    {
+      gdb_assert (is_integral_type (type)
+                 || is_fixed_point_type (type));
+
+      gdb_mpz vz;
+      vz.read (gdb::make_array_view (value_contents (value),
+                                    TYPE_LENGTH (type)),
+              type_byte_order (type), type->is_unsigned ());
+      mpq_set_z (result.val, vz.val);
+
+      if (is_fixed_point_type (type))
+       mpq_mul (result.val, result.val,
+                type->fixed_point_scaling_factor ().val);
+    }
+
+  return result;
+}
+
 /* Assuming that TO_TYPE is a fixed point type, return a value
    corresponding to the cast of FROM_VAL to that type.  */
 
@@ -342,34 +375,14 @@ value_cast_to_fixed_point (struct type *to_type, struct value *from_val)
   if (from_type == to_type)
     return from_val;
 
-  gdb_mpq vq;
-
-  /* Extract the value as a rational number.  */
-
-  if (is_floating_type (from_type))
-    {
-      double d = target_float_to_host_double (value_contents (from_val),
-                                             from_type);
-      mpq_set_d (vq.val, d);
-    }
-
-  else if (is_integral_type (from_type) || is_fixed_point_type (from_type))
-    {
-      gdb_mpz vz;
-
-      vz.read (gdb::make_array_view (value_contents (from_val),
-                                    TYPE_LENGTH (from_type)),
-              type_byte_order (from_type), from_type->is_unsigned ());
-      mpq_set_z (vq.val, vz.val);
-
-      if (is_fixed_point_type (from_type))
-       mpq_mul (vq.val, vq.val, from_type->fixed_point_scaling_factor ().val);
-    }
-
-  else
+  if (!is_floating_type (from_type)
+      && !is_integral_type (from_type)
+      && !is_fixed_point_type (from_type))
     error (_("Invalid conversion from type %s to fixed point type %s"),
           from_type->name (), to_type->name ());
 
+  gdb_mpq vq = value_to_gdb_mpq (from_val);
+
   /* Divide that value by the scaling factor to obtain the unscaled
      value, first in rational form, and then in integer form.  */
 
@@ -402,8 +415,25 @@ value_cast (struct type *type, struct value *arg2)
 
   int convert_to_boolean = 0;
 
-  if (value_type (arg2) == type)
-    return arg2;
+  /* TYPE might be equal in meaning to the existing type of ARG2, but for
+     many reasons, might be a different type object (e.g. TYPE might be a
+     gdbarch owned type, while VALUE_TYPE (ARG2) could be an objfile owned
+     type).
+
+     In this case we want to preserve the LVAL of ARG2 as this allows the
+     resulting value to be used in more places.  We do this by calling
+     VALUE_COPY if appropriate.  */
+  if (types_deeply_equal (value_type (arg2), type))
+    {
+      /* If the types are exactly equal then we can avoid creating a new
+        value completely.  */
+      if (value_type (arg2) != type)
+       {
+         arg2 = value_copy (arg2);
+         deprecated_set_value_type (arg2, type);
+       }
+      return arg2;
+    }
 
   if (is_fixed_point_type (type))
     return value_cast_to_fixed_point (type, arg2);
@@ -577,7 +607,7 @@ value_cast (struct type *type, struct value *arg2)
         otherwise occur when dealing with a target having two byte
         pointers and four byte addresses.  */
 
-      int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
+      int addr_bit = gdbarch_addr_bit (type2->arch ());
       LONGEST longest = value_as_long (arg2);
 
       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
@@ -1027,7 +1057,7 @@ read_value_memory (struct value *val, LONGEST bit_offset,
       enum target_xfer_status status;
       ULONGEST xfered_partial;
 
-      status = target_xfer_partial (current_top_target (),
+      status = target_xfer_partial (current_inferior ()->top_target (),
                                    object, NULL,
                                    buffer + xfered_total * unit_size, NULL,
                                    memaddr + xfered_total,
@@ -1087,7 +1117,7 @@ value_assign (struct value *toval, struct value *fromval)
     {
     case lval_internalvar:
       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
-      return value_of_internalvar (get_type_arch (type),
+      return value_of_internalvar (type->arch (),
                                   VALUE_INTERNALVAR (toval));
 
     case lval_internalvar_component:
@@ -1187,7 +1217,7 @@ value_assign (struct value *toval, struct value *fromval)
          {
            struct value *parent = value_parent (toval);
            LONGEST offset = value_offset (parent) + value_offset (toval);
-           int changed_len;
+           size_t changed_len;
            gdb_byte buffer[sizeof (LONGEST)];
            int optim, unavail;
 
@@ -1196,13 +1226,13 @@ value_assign (struct value *toval, struct value *fromval)
                           + HOST_CHAR_BIT - 1)
                          / HOST_CHAR_BIT;
 
-           if (changed_len > (int) sizeof (LONGEST))
+           if (changed_len > sizeof (LONGEST))
              error (_("Can't handle bitfields which "
                       "don't fit in a %d bit word."),
                     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
 
            if (!get_frame_register_bytes (frame, value_reg, offset,
-                                          changed_len, buffer,
+                                          {buffer, changed_len},
                                           &optim, &unavail))
              {
                if (optim)
@@ -1217,7 +1247,7 @@ value_assign (struct value *toval, struct value *fromval)
                          value_bitpos (toval), value_bitsize (toval));
 
            put_frame_register_bytes (frame, value_reg, offset,
-                                     changed_len, buffer);
+                                     {buffer, changed_len});
          }
        else
          {
@@ -1233,10 +1263,12 @@ value_assign (struct value *toval, struct value *fromval)
              }
            else
              {
+               gdb::array_view<const gdb_byte> contents
+                 = gdb::make_array_view (value_contents (fromval),
+                                         TYPE_LENGTH (type));
                put_frame_register_bytes (frame, value_reg,
                                          value_offset (toval),
-                                         TYPE_LENGTH (type),
-                                         value_contents (fromval));
+                                         contents);
              }
          }
 
@@ -1271,7 +1303,8 @@ value_assign (struct value *toval, struct value *fromval)
     case lval_register:
     case lval_computed:
 
-      gdb::observers::target_changed.notify (current_top_target ());
+      gdb::observers::target_changed.notify
+       (current_inferior ()->top_target ());
 
       /* Having destroyed the frame cache, restore the selected
         frame.  */
@@ -3297,7 +3330,7 @@ enum_constant_from_type (struct type *type, const char *name)
   int name_len = strlen (name);
 
   gdb_assert (type->code () == TYPE_CODE_ENUM
-             && TYPE_DECLARED_CLASS (type));
+             && type->is_declared_class ());
 
   for (i = TYPE_N_BASECLASSES (type); i < type->num_fields (); ++i)
     {