2009-10-19 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / valops.c
index 1f71a143c517288440e9ea9bdac26be84179aa12..012ea6a4297541bd73cd8451af39c33b5c464ad7 100644 (file)
@@ -422,17 +422,18 @@ value_cast (struct type *type, struct value *arg2)
     return value_from_double (type, value_as_double (arg2));
   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
     {
+      enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
       int dec_len = TYPE_LENGTH (type);
       gdb_byte dec[16];
 
       if (code2 == TYPE_CODE_FLT)
-       decimal_from_floating (arg2, dec, dec_len);
+       decimal_from_floating (arg2, dec, dec_len, byte_order);
       else if (code2 == TYPE_CODE_DECFLOAT)
        decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
-                        dec, dec_len);
+                        byte_order, dec, dec_len, byte_order);
       else
        /* The only option left is an integral type.  */
-       decimal_from_integral (arg2, dec, dec_len);
+       decimal_from_integral (arg2, dec, dec_len, byte_order);
 
       return value_from_decfloat (type, dec);
     }
@@ -450,8 +451,9 @@ value_cast (struct type *type, struct value *arg2)
          sees a cast as a simple reinterpretation of the pointer's
          bits.  */
       if (code2 == TYPE_CODE_PTR)
-        longest = extract_unsigned_integer (value_contents (arg2),
-                                            TYPE_LENGTH (type2));
+        longest = extract_unsigned_integer
+                   (value_contents (arg2), TYPE_LENGTH (type2),
+                    gdbarch_byte_order (get_type_arch (type2)));
       else
         longest = value_as_long (arg2);
       return value_from_longest (type, convert_to_boolean ?
@@ -471,7 +473,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 (current_gdbarch);
+      int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
 
       LONGEST longest = value_as_long (arg2);
       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
@@ -511,7 +513,7 @@ value_cast (struct type *type, struct value *arg2)
     return value_at_lazy (type, value_address (arg2));
   else if (code1 == TYPE_CODE_VOID)
     {
-      return value_zero (builtin_type_void, not_lval);
+      return value_zero (type, not_lval);
     }
   else
     {
@@ -541,8 +543,9 @@ value_one (struct type *type, enum lval_type lv)
 
   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
     {
+      enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
       gdb_byte v[16];
-      decimal_from_string (v, TYPE_LENGTH (type), "1");
+      decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
       val = value_from_decfloat (type, v);
     }
   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
@@ -562,6 +565,32 @@ value_one (struct type *type, enum lval_type lv)
   return val;
 }
 
+/* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
+
+static struct value *
+get_value_at (struct type *type, CORE_ADDR addr, int lazy)
+{
+  struct value *val;
+
+  if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
+    error (_("Attempt to dereference a generic pointer."));
+
+  if (lazy)
+    {
+      val = allocate_value_lazy (type);
+    }
+  else
+    {
+      val = allocate_value (type);
+      read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type));
+    }
+
+  VALUE_LVAL (val) = lval_memory;
+  set_value_address (val, addr);
+
+  return val;
+}
+
 /* Return a value with type TYPE located at ADDR.
 
    Call value_at only if the data needs to be fetched immediately;
@@ -577,19 +606,7 @@ value_one (struct type *type, enum lval_type lv)
 struct value *
 value_at (struct type *type, CORE_ADDR addr)
 {
-  struct value *val;
-
-  if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
-    error (_("Attempt to dereference a generic pointer."));
-
-  val = allocate_value (type);
-
-  read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type));
-
-  VALUE_LVAL (val) = lval_memory;
-  set_value_address (val, addr);
-
-  return val;
+  return get_value_at (type, addr, 0);
 }
 
 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
@@ -597,17 +614,7 @@ value_at (struct type *type, CORE_ADDR addr)
 struct value *
 value_at_lazy (struct type *type, CORE_ADDR addr)
 {
-  struct value *val;
-
-  if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
-    error (_("Attempt to dereference a generic pointer."));
-
-  val = allocate_value_lazy (type);
-
-  VALUE_LVAL (val) = lval_memory;
-  set_value_address (val, addr);
-
-  return val;
+  return get_value_at (type, addr, 1);
 }
 
 /* Called only from the value_contents and value_contents_all()
@@ -629,13 +636,36 @@ value_fetch_lazy (struct value *val)
 {
   gdb_assert (value_lazy (val));
   allocate_value_contents (val);
-  if (VALUE_LVAL (val) == lval_memory)
+  if (value_bitsize (val))
+    {
+      /* To read a lazy bitfield, read the entire enclosing value.  This
+        prevents reading the same block of (possibly volatile) memory once
+         per bitfield.  It would be even better to read only the containing
+         word, but we have no way to record that just specific bits of a
+         value have been fetched.  */
+      struct type *type = check_typedef (value_type (val));
+      enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
+      struct value *parent = value_parent (val);
+      LONGEST offset = value_offset (val);
+      LONGEST num = unpack_bits_as_long (value_type (val),
+                                        value_contents (parent) + offset,
+                                        value_bitpos (val),
+                                        value_bitsize (val));
+      int length = TYPE_LENGTH (type);
+      store_signed_integer (value_contents_raw (val), length, byte_order, num);
+    }
+  else if (VALUE_LVAL (val) == lval_memory)
     {
       CORE_ADDR addr = value_address (val);
       int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
 
       if (length)
-       read_memory (addr, value_contents_all_raw (val), length);
+       {
+         if (value_stack (val))
+           read_stack (addr, value_contents_all_raw (val), length);
+         else
+           read_memory (addr, value_contents_all_raw (val), length);
+       }
     }
   else if (VALUE_LVAL (val) == lval_register)
     {
@@ -702,8 +732,9 @@ value_fetch_lazy (struct value *val)
                fprintf_unfiltered (gdb_stdlog, " register=%d",
                                    VALUE_REGNUM (new_val));
              else if (VALUE_LVAL (new_val) == lval_memory)
-               fprintf_unfiltered (gdb_stdlog, " address=0x%s",
-                                   paddr_nz (value_address (new_val)));
+               fprintf_unfiltered (gdb_stdlog, " address=%s",
+                                   paddress (gdbarch,
+                                             value_address (new_val)));
              else
                fprintf_unfiltered (gdb_stdlog, " computed");
 
@@ -796,21 +827,30 @@ value_assign (struct value *toval, struct value *fromval)
 
        if (value_bitsize (toval))
          {
-           /* We assume that the argument to read_memory is in units
-              of host chars.  FIXME: Is that correct?  */
+           struct value *parent = value_parent (toval);
+           changed_addr = value_address (parent) + value_offset (toval);
+
            changed_len = (value_bitpos (toval)
                           + value_bitsize (toval)
                           + HOST_CHAR_BIT - 1)
              / HOST_CHAR_BIT;
 
+           /* If we can read-modify-write exactly the size of the
+              containing type (e.g. short or int) then do so.  This
+              is safer for volatile bitfields mapped to hardware
+              registers.  */
+           if (changed_len < TYPE_LENGTH (type)
+               && TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
+               && ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
+             changed_len = TYPE_LENGTH (type);
+
            if (changed_len > (int) sizeof (LONGEST))
              error (_("Can't handle bitfields which don't fit in a %d bit word."),
                     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
 
-           read_memory (value_address (toval), buffer, changed_len);
-           modify_field (buffer, value_as_long (fromval),
+           read_memory (changed_addr, buffer, changed_len);
+           modify_field (type, buffer, value_as_long (fromval),
                          value_bitpos (toval), value_bitsize (toval));
-           changed_addr = value_address (toval);
            dest_buffer = buffer;
          }
        else
@@ -853,6 +893,8 @@ value_assign (struct value *toval, struct value *fromval)
          {
            if (value_bitsize (toval))
              {
+               struct value *parent = value_parent (toval);
+               int offset = value_offset (parent) + value_offset (toval);
                int changed_len;
                gdb_byte buffer[sizeof (LONGEST)];
 
@@ -865,16 +907,13 @@ value_assign (struct value *toval, struct value *fromval)
                  error (_("Can't handle bitfields which don't fit in a %d bit word."),
                         (int) sizeof (LONGEST) * HOST_CHAR_BIT);
 
-               get_frame_register_bytes (frame, value_reg,
-                                         value_offset (toval),
+               get_frame_register_bytes (frame, value_reg, offset,
                                          changed_len, buffer);
 
-               modify_field (buffer, value_as_long (fromval),
-                             value_bitpos (toval), 
-                             value_bitsize (toval));
+               modify_field (type, buffer, value_as_long (fromval),
+                             value_bitpos (toval), value_bitsize (toval));
 
-               put_frame_register_bytes (frame, value_reg,
-                                         value_offset (toval),
+               put_frame_register_bytes (frame, value_reg, offset,
                                          changed_len, buffer);
              }
            else
@@ -2947,7 +2986,7 @@ value_slice (struct value *array, int lowbound, int length)
          else if (element > 0)
            {
              int j = i % TARGET_CHAR_BIT;
-             if (gdbarch_bits_big_endian (current_gdbarch))
+             if (gdbarch_bits_big_endian (get_type_arch (array_type)))
                j = TARGET_CHAR_BIT - 1 - j;
              value_contents_raw (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
            }
This page took 0.077733 seconds and 4 git commands to generate.