gdb:
[deliverable/binutils-gdb.git] / gdb / value.c
index dc2e8bc3093c484eeec3a4365bfa498551a3b60e..381318b987c36cee05ea58ff09c361b962fa424c 100644 (file)
@@ -2,7 +2,7 @@
 
    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
    1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-   2009 Free Software Foundation, Inc.
+   2009, 2010 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -108,6 +108,11 @@ struct value
      gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
   int bitpos;
 
+  /* Only used for bitfields; the containing value.  This allows a
+     single read from the target when displaying multiple
+     bitfields.  */
+  struct value *parent;
+
   /* Frame register value is relative to.  This will be described in
      the lval enum above as "lval_register".  */
   struct frame_id frame_id;
@@ -191,9 +196,20 @@ struct value
   /* If value is a variable, is it initialized or not.  */
   int initialized;
 
+  /* If value is from the stack.  If this is set, read_stack will be
+     used instead of read_memory to enable extra caching.  */
+  int stack;
+
   /* Actual contents of the value.  Target byte-order.  NULL or not
      valid if lazy is nonzero.  */
   gdb_byte *contents;
+
+  /* The number of references to this value.  When a value is created,
+     the value chain holds a reference, so REFERENCE_COUNT is 1.  If
+     release_value is called, this value is removed from the chain but
+     the caller of release_value now has a reference to this value.
+     The caller must arrange for a call to value_free later.  */
+  int reference_count;
 };
 
 /* Prototypes for local functions. */
@@ -238,7 +254,14 @@ struct value *
 allocate_value_lazy (struct type *type)
 {
   struct value *val;
-  struct type *atype = check_typedef (type);
+
+  /* Call check_typedef on our type to make sure that, if TYPE
+     is a TYPE_CODE_TYPEDEF, its length is set to the length
+     of the target type instead of zero.  However, we do not
+     replace the typedef type by the target type, because we want
+     to keep the typedef in order to be able to set the VAL's type
+     description correctly.  */
+  check_typedef (type);
 
   val = (struct value *) xzalloc (sizeof (struct value));
   val->contents = NULL;
@@ -259,6 +282,10 @@ allocate_value_lazy (struct type *type)
   val->pointed_to_offset = 0;
   val->modifiable = 1;
   val->initialized = 1;  /* Default to initialized.  */
+
+  /* Values start out on the all_values chain.  */
+  val->reference_count = 1;
+
   return val;
 }
 
@@ -277,6 +304,7 @@ struct value *
 allocate_value (struct type *type)
 {
   struct value *val = allocate_value_lazy (type);
+
   allocate_value_contents (val);
   val->lazy = 0;
   return val;
@@ -293,32 +321,8 @@ allocate_repeat_value (struct type *type, int count)
      done with it.  */
   struct type *array_type
     = lookup_array_range_type (type, low_bound, count + low_bound - 1);
-  return allocate_value (array_type);
-}
-
-/* Needed if another module needs to maintain its on list of values.  */
-void
-value_prepend_to_list (struct value **head, struct value *val)
-{
-  val->next = *head;
-  *head = val;
-}
-
-/* Needed if another module needs to maintain its on list of values.  */
-void
-value_remove_from_list (struct value **head, struct value *val)
-{
-  struct value *prev;
 
-  if (*head == val)
-    *head = (*head)->next;
-  else
-    for (prev = *head; prev->next; prev = prev->next)
-      if (prev->next == val)
-      {
-       prev->next = val->next;
-       break;
-      }
+  return allocate_value (array_type);
 }
 
 struct value *
@@ -327,6 +331,7 @@ allocate_computed_value (struct type *type,
                          void *closure)
 {
   struct value *v = allocate_value (type);
+
   VALUE_LVAL (v) = lval_computed;
   v->location.computed.funcs = funcs;
   v->location.computed.closure = closure;
@@ -344,7 +349,7 @@ value_next (struct value *value)
 }
 
 struct type *
-value_type (struct value *value)
+value_type (const struct value *value)
 {
   return value->type;
 }
@@ -355,7 +360,7 @@ deprecated_set_value_type (struct value *value, struct type *type)
 }
 
 int
-value_offset (struct value *value)
+value_offset (const struct value *value)
 {
   return value->offset;
 }
@@ -366,7 +371,7 @@ set_value_offset (struct value *value, int offset)
 }
 
 int
-value_bitpos (struct value *value)
+value_bitpos (const struct value *value)
 {
   return value->bitpos;
 }
@@ -377,7 +382,7 @@ set_value_bitpos (struct value *value, int bit)
 }
 
 int
-value_bitsize (struct value *value)
+value_bitsize (const struct value *value)
 {
   return value->bitsize;
 }
@@ -387,6 +392,12 @@ set_value_bitsize (struct value *value, int bit)
   value->bitsize = bit;
 }
 
+struct value *
+value_parent (struct value *value)
+{
+  return value->parent;
+}
+
 gdb_byte *
 value_contents_raw (struct value *value)
 {
@@ -407,14 +418,29 @@ value_enclosing_type (struct value *value)
   return value->enclosing_type;
 }
 
+static void
+require_not_optimized_out (struct value *value)
+{
+  if (value->optimized_out)
+    error (_("value has been optimized out"));
+}
+
 const gdb_byte *
-value_contents_all (struct value *value)
+value_contents_for_printing (struct value *value)
 {
   if (value->lazy)
     value_fetch_lazy (value);
   return value->contents;
 }
 
+const gdb_byte *
+value_contents_all (struct value *value)
+{
+  const gdb_byte *result = value_contents_for_printing (value);
+  require_not_optimized_out (value);
+  return result;
+}
+
 int
 value_lazy (struct value *value)
 {
@@ -427,10 +453,24 @@ set_value_lazy (struct value *value, int val)
   value->lazy = val;
 }
 
+int
+value_stack (struct value *value)
+{
+  return value->stack;
+}
+
+void
+set_value_stack (struct value *value, int val)
+{
+  value->stack = val;
+}
+
 const gdb_byte *
 value_contents (struct value *value)
 {
-  return value_contents_writeable (value);
+  const gdb_byte *result = value_contents_writeable (value);
+  require_not_optimized_out (value);
+  return result;
 }
 
 gdb_byte *
@@ -473,6 +513,29 @@ set_value_optimized_out (struct value *value, int val)
   value->optimized_out = val;
 }
 
+int
+value_entirely_optimized_out (const struct value *value)
+{
+  if (!value->optimized_out)
+    return 0;
+  if (value->lval != lval_computed
+      || !value->location.computed.funcs->check_validity)
+    return 1;
+  return !value->location.computed.funcs->check_any_valid (value);
+}
+
+int
+value_bits_valid (const struct value *value, int offset, int length)
+{
+  if (value == NULL || !value->optimized_out)
+    return 1;
+  if (value->lval != lval_computed
+      || !value->location.computed.funcs->check_validity)
+    return 0;
+  return value->location.computed.funcs->check_validity (value, offset,
+                                                        length);
+}
+
 int
 value_embedded_offset (struct value *value)
 {
@@ -506,9 +569,9 @@ value_computed_funcs (struct value *v)
 }
 
 void *
-value_computed_closure (struct value *v)
+value_computed_closure (const struct value *v)
 {
-  gdb_assert (VALUE_LVAL (v) == lval_computed);
+  gdb_assert (v->lval == lval_computed);
 
   return v->location.computed.closure;
 }
@@ -583,11 +646,34 @@ value_mark (void)
   return all_values;
 }
 
+/* Take a reference to VAL.  VAL will not be deallocated until all
+   references are released.  */
+
+void
+value_incref (struct value *val)
+{
+  val->reference_count++;
+}
+
+/* Release a reference to VAL, which was acquired with value_incref.
+   This function is also called to deallocate values from the value
+   chain.  */
+
 void
 value_free (struct value *val)
 {
   if (val)
     {
+      gdb_assert (val->reference_count > 0);
+      val->reference_count--;
+      if (val->reference_count > 0)
+       return;
+
+      /* If there's an associated parent value, drop our reference to
+        it.  */
+      if (val->parent != NULL)
+       value_free (val->parent);
+
       if (VALUE_LVAL (val) == lval_computed)
        {
          struct lval_funcs *funcs = val->location.computed.funcs;
@@ -618,7 +704,8 @@ value_free_to_mark (struct value *mark)
 }
 
 /* Free all the values that have been allocated (except for those released).
-   Called after each command, successful or not.  */
+   Call after each command, successful or not.
+   In practice this is called before each command, which is sufficient.  */
 
 void
 free_all_values (void)
@@ -635,6 +722,20 @@ free_all_values (void)
   all_values = 0;
 }
 
+/* Frees all the elements in a chain of values.  */
+
+void
+free_value_chain (struct value *v)
+{
+  struct value *next;
+
+  for (; v; v = next)
+    {
+      next = value_next (v);
+      value_free (v);
+    }
+}
+
 /* Remove VAL from the chain all_values
    so it will not be freed automatically.  */
 
@@ -646,6 +747,7 @@ release_value (struct value *val)
   if (all_values == val)
     {
       all_values = val->next;
+      val->next = NULL;
       return;
     }
 
@@ -654,6 +756,7 @@ release_value (struct value *val)
       if (v->next == val)
        {
          v->next = val->next;
+         val->next = NULL;
          break;
        }
     }
@@ -710,6 +813,9 @@ value_copy (struct value *arg)
              TYPE_LENGTH (value_enclosing_type (arg)));
 
     }
+  val->parent = arg->parent;
+  if (val->parent)
+    value_incref (val->parent);
   if (VALUE_LVAL (val) == lval_computed)
     {
       struct lval_funcs *funcs = val->location.computed.funcs;
@@ -720,16 +826,37 @@ value_copy (struct value *arg)
   return val;
 }
 
+/* Return a version of ARG that is non-lvalue.  */
+
+struct value *
+value_non_lval (struct value *arg)
+{
+  if (VALUE_LVAL (arg) != not_lval)
+    {
+      struct type *enc_type = value_enclosing_type (arg);
+      struct value *val = allocate_value (enc_type);
+
+      memcpy (value_contents_all_raw (val), value_contents_all (arg),
+             TYPE_LENGTH (enc_type));
+      val->type = arg->type;
+      set_value_embedded_offset (val, value_embedded_offset (arg));
+      set_value_pointed_to_offset (val, value_pointed_to_offset (arg));
+      return val;
+    }
+   return arg;
+}
+
 void
-set_value_component_location (struct value *component, struct value *whole)
+set_value_component_location (struct value *component,
+                             const struct value *whole)
 {
-  if (VALUE_LVAL (whole) == lval_internalvar)
+  if (whole->lval == lval_internalvar)
     VALUE_LVAL (component) = lval_internalvar_component;
   else
-    VALUE_LVAL (component) = VALUE_LVAL (whole);
+    VALUE_LVAL (component) = whole->lval;
 
   component->location = whole->location;
-  if (VALUE_LVAL (whole) == lval_computed)
+  if (whole->lval == lval_computed)
     {
       struct lval_funcs *funcs = whole->location.computed.funcs;
 
@@ -770,7 +897,8 @@ record_latest_value (struct value *val)
   if (i == 0)
     {
       struct value_history_chunk *new
-      = (struct value_history_chunk *)
+       = (struct value_history_chunk *)
+
       xmalloc (sizeof (struct value_history_chunk));
       memset (new->values, 0, sizeof new->values);
       new->next = value_history_chain;
@@ -847,6 +975,7 @@ show_values (char *num_exp, int from_tty)
   for (i = num; i < num + 10 && i <= value_history_count; i++)
     {
       struct value_print_options opts;
+
       val = access_value_history (i);
       printf_filtered (("$%d = "), i);
       get_user_print_options (&opts);
@@ -897,8 +1026,11 @@ struct internalvar
       /* The internal variable holds a GDB internal convenience function.  */
       INTERNALVAR_FUNCTION,
 
-      /* The variable holds a simple scalar value.  */
-      INTERNALVAR_SCALAR,
+      /* The variable holds an integer value.  */
+      INTERNALVAR_INTEGER,
+
+      /* The variable holds a pointer value.  */
+      INTERNALVAR_POINTER,
 
       /* The variable holds a GDB-provided string.  */
       INTERNALVAR_STRING,
@@ -921,19 +1053,22 @@ struct internalvar
          int canonical;
        } fn;
 
-      /* A scalar value used with INTERNALVAR_SCALAR.  */
+      /* An integer value used with INTERNALVAR_INTEGER.  */
       struct
         {
          /* If type is non-NULL, it will be used as the type to generate
             a value for this internal variable.  If type is NULL, a default
             integer type for the architecture is used.  */
          struct type *type;
-         union
-           {
-             LONGEST l;    /* Used with TYPE_CODE_INT and NULL types.  */
-             CORE_ADDR a;  /* Used with TYPE_CODE_PTR types.  */
-           } val;
-        } scalar;
+         LONGEST val;
+        } integer;
+
+      /* A pointer value used with INTERNALVAR_POINTER.  */
+      struct
+        {
+         struct type *type;
+         CORE_ADDR val;
+        } pointer;
 
       /* A string value used with INTERNALVAR_STRING.  */
       char *string;
@@ -1001,6 +1136,7 @@ struct internalvar *
 create_internalvar (const char *name)
 {
   struct internalvar *var;
+
   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
   var->name = concat (name, (char *)NULL);
   var->kind = INTERNALVAR_VOID;
@@ -1018,6 +1154,7 @@ struct internalvar *
 create_internalvar_type_lazy (char *name, internalvar_make_value fun)
 {
   struct internalvar *var = create_internalvar (name);
+
   var->kind = INTERNALVAR_MAKE_VALUE;
   var->u.make_value = fun;
   return var;
@@ -1059,16 +1196,16 @@ value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
       val = allocate_value (builtin_type (gdbarch)->internal_fn);
       break;
 
-    case INTERNALVAR_SCALAR:
-      if (!var->u.scalar.type)
+    case INTERNALVAR_INTEGER:
+      if (!var->u.integer.type)
        val = value_from_longest (builtin_type (gdbarch)->builtin_int,
-                                 var->u.scalar.val.l);
-      else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT)
-       val = value_from_longest (var->u.scalar.type, var->u.scalar.val.l);
-      else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_PTR)
-       val = value_from_pointer (var->u.scalar.type, var->u.scalar.val.a);
+                                 var->u.integer.val);
       else
-        internal_error (__FILE__, __LINE__, "bad type");
+       val = value_from_longest (var->u.integer.type, var->u.integer.val);
+      break;
+
+    case INTERNALVAR_POINTER:
+      val = value_from_pointer (var->u.pointer.type, var->u.pointer.val);
       break;
 
     case INTERNALVAR_STRING:
@@ -1122,14 +1259,9 @@ get_internalvar_integer (struct internalvar *var, LONGEST *result)
 {
   switch (var->kind)
     {
-    case INTERNALVAR_SCALAR:
-      if (var->u.scalar.type == NULL
-         || TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT)
-       {
-         *result = var->u.scalar.val.l;
-         return 1;
-       }
-      /* Fall through.  */
+    case INTERNALVAR_INTEGER:
+      *result = var->u.integer.val;
+      return 1;
 
     default:
       return 0;
@@ -1201,15 +1333,15 @@ set_internalvar (struct internalvar *var, struct value *val)
       break;
 
     case TYPE_CODE_INT:
-      new_kind = INTERNALVAR_SCALAR;
-      new_data.scalar.type = value_type (val);
-      new_data.scalar.val.l = value_as_long (val);
+      new_kind = INTERNALVAR_INTEGER;
+      new_data.integer.type = value_type (val);
+      new_data.integer.val = value_as_long (val);
       break;
 
     case TYPE_CODE_PTR:
-      new_kind = INTERNALVAR_SCALAR;
-      new_data.scalar.type = value_type (val);
-      new_data.scalar.val.a = value_as_address (val);
+      new_kind = INTERNALVAR_POINTER;
+      new_data.pointer.type = value_type (val);
+      new_data.pointer.val = value_as_address (val);
       break;
 
     default:
@@ -1246,9 +1378,9 @@ set_internalvar_integer (struct internalvar *var, LONGEST l)
   /* Clean up old contents.  */
   clear_internalvar (var);
 
-  var->kind = INTERNALVAR_SCALAR;
-  var->u.scalar.type = NULL;
-  var->u.scalar.val.l = l;
+  var->kind = INTERNALVAR_INTEGER;
+  var->u.integer.type = NULL;
+  var->u.integer.val = l;
 }
 
 void
@@ -1306,6 +1438,7 @@ create_internal_function (const char *name,
                          internal_function_fn handler, void *cookie)
 {
   struct internal_function *ifn = XNEW (struct internal_function);
+
   ifn->name = xstrdup (name);
   ifn->handler = handler;
   ifn->cookie = cookie;
@@ -1382,7 +1515,7 @@ add_internal_function (const char *name, const char *doc,
 /* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
    prevent cycles / duplicates.  */
 
-static void
+void
 preserve_one_value (struct value *value, struct objfile *objfile,
                    htab_t copied_types)
 {
@@ -1403,10 +1536,16 @@ preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
 {
   switch (var->kind)
     {
-    case INTERNALVAR_SCALAR:
-      if (var->u.scalar.type && TYPE_OBJFILE (var->u.scalar.type) == objfile)
-       var->u.scalar.type
-         = copy_type_recursive (objfile, var->u.scalar.type, copied_types);
+    case INTERNALVAR_INTEGER:
+      if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
+       var->u.integer.type
+         = copy_type_recursive (objfile, var->u.integer.type, copied_types);
+      break;
+
+    case INTERNALVAR_POINTER:
+      if (TYPE_OBJFILE (var->u.pointer.type) == objfile)
+       var->u.pointer.type
+         = copy_type_recursive (objfile, var->u.pointer.type, copied_types);
       break;
 
     case INTERNALVAR_VALUE:
@@ -1427,7 +1566,6 @@ preserve_values (struct objfile *objfile)
   htab_t copied_types;
   struct value_history_chunk *cur;
   struct internalvar *var;
-  struct value *val;
   int i;
 
   /* Create the hash table.  We allocate on the objfile's obstack, since
@@ -1442,8 +1580,7 @@ preserve_values (struct objfile *objfile)
   for (var = internalvars; var; var = var->next)
     preserve_one_internalvar (var, objfile, copied_types);
 
-  for (val = values_in_python; val; val = val->next)
-    preserve_one_value (val, objfile, copied_types);
+  preserve_python_values (objfile, copied_types);
 
   htab_delete (copied_types);
 }
@@ -1626,6 +1763,7 @@ value_as_address (struct value *val)
 LONGEST
 unpack_long (struct type *type, const gdb_byte *valaddr)
 {
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   enum type_code code = TYPE_CODE (type);
   int len = TYPE_LENGTH (type);
   int nosign = TYPE_UNSIGNED (type);
@@ -1642,9 +1780,9 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
     case TYPE_CODE_RANGE:
     case TYPE_CODE_MEMBERPTR:
       if (nosign)
-       return extract_unsigned_integer (valaddr, len);
+       return extract_unsigned_integer (valaddr, len, byte_order);
       else
-       return extract_signed_integer (valaddr, len);
+       return extract_signed_integer (valaddr, len, byte_order);
 
     case TYPE_CODE_FLT:
       return extract_typed_floating (valaddr, type);
@@ -1652,7 +1790,7 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
     case TYPE_CODE_DECFLOAT:
       /* libdecnumber has a function to convert from decimal to integer, but
         it doesn't work when the decimal number has a fractional part.  */
-      return decimal_to_doublest (valaddr, len);
+      return decimal_to_doublest (valaddr, len, byte_order);
 
     case TYPE_CODE_PTR:
     case TYPE_CODE_REF:
@@ -1675,6 +1813,7 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
 DOUBLEST
 unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
 {
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   enum type_code code;
   int len;
   int nosign;
@@ -1712,7 +1851,7 @@ unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
       return extract_typed_floating (valaddr, type);
     }
   else if (code == TYPE_CODE_DECFLOAT)
-    return decimal_to_doublest (valaddr, len);
+    return decimal_to_doublest (valaddr, len, byte_order);
   else if (nosign)
     {
       /* Unsigned -- be sure we compensate for signed LONGEST.  */
@@ -1747,7 +1886,7 @@ unpack_pointer (struct type *type, const gdb_byte *valaddr)
 }
 
 \f
-/* Get the value of the FIELDN'th field (which must be static) of
+/* Get the value of the FIELDNO'th field (which must be static) of
    TYPE.  Return NULL if the field doesn't exist or has been
    optimized out. */
 
@@ -1756,41 +1895,41 @@ value_static_field (struct type *type, int fieldno)
 {
   struct value *retval;
 
-  if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
+  switch (TYPE_FIELD_LOC_KIND (type, fieldno))
     {
-      retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
-                        TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
-    }
-  else
+    case FIELD_LOC_KIND_PHYSADDR:
+      retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
+                             TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
+      break;
+    case FIELD_LOC_KIND_PHYSNAME:
     {
       char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
+      /*TYPE_FIELD_NAME (type, fieldno);*/
       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
+
       if (sym == NULL)
        {
-         /* With some compilers, e.g. HP aCC, static data members are reported
-            as non-debuggable symbols */
-         struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
+         /* With some compilers, e.g. HP aCC, static data members are
+            reported as non-debuggable symbols */
+         struct minimal_symbol *msym = lookup_minimal_symbol (phys_name,
+                                                              NULL, NULL);
+
          if (!msym)
            return NULL;
          else
            {
-             retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
-                                SYMBOL_VALUE_ADDRESS (msym));
+             retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
+                                     SYMBOL_VALUE_ADDRESS (msym));
            }
        }
       else
-       {
-         /* SYM should never have a SYMBOL_CLASS which will require
-            read_var_value to use the FRAME parameter.  */
-         if (symbol_read_needs_frame (sym))
-           warning (_("static field's value depends on the current "
-                    "frame - bad debug info?"));
-         retval = read_var_value (sym, NULL);
-       }
-      if (retval && VALUE_LVAL (retval) == lval_memory)
-       SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
-                           value_address (retval));
+       retval = value_of_variable (sym, NULL);
+      break;
+    }
+    default:
+      gdb_assert_not_reached ("unexpected field location kind");
     }
+
   return retval;
 }
 
@@ -1826,19 +1965,41 @@ value_primitive_field (struct value *arg1, int offset,
   CHECK_TYPEDEF (arg_type);
   type = TYPE_FIELD_TYPE (arg_type, fieldno);
 
+  /* Call check_typedef on our type to make sure that, if TYPE
+     is a TYPE_CODE_TYPEDEF, its length is set to the length
+     of the target type instead of zero.  However, we do not
+     replace the typedef type by the target type, because we want
+     to keep the typedef in order to be able to print the type
+     description correctly.  */
+  check_typedef (type);
+
   /* Handle packed fields */
 
   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
     {
-      v = value_from_longest (type,
-                             unpack_field_as_long (arg_type,
-                                                   value_contents (arg1)
-                                                   + offset,
-                                                   fieldno));
-      v->bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
+      /* Create a new value for the bitfield, with bitpos and bitsize
+        set.  If possible, arrange offset and bitpos so that we can
+        do a single aligned read of the size of the containing type.
+        Otherwise, adjust offset to the byte containing the first
+        bit.  Assume that the address, offset, and embedded offset
+        are sufficiently aligned.  */
+      int bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
+      int container_bitsize = TYPE_LENGTH (type) * 8;
+
+      v = allocate_value_lazy (type);
       v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
-      v->offset = value_offset (arg1) + offset
-       + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
+      if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
+         && TYPE_LENGTH (type) <= (int) sizeof (LONGEST))
+       v->bitpos = bitpos % container_bitsize;
+      else
+       v->bitpos = bitpos % 8;
+      v->offset = (value_embedded_offset (arg1)
+                  + offset
+                  + (bitpos - v->bitpos) / 8);
+      v->parent = arg1;
+      value_incref (v->parent);
+      if (!value_lazy (arg1))
+       value_fetch_lazy (v);
     }
   else if (fieldno < TYPE_N_BASECLASSES (arg_type))
     {
@@ -1963,8 +2124,9 @@ value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *ty
 }
 
 \f
-/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
-   VALADDR.
+/* Unpack a bitfield of the specified FIELD_TYPE, from the anonymous
+   object at VALADDR.  The bitfield starts at BITPOS bits and contains
+   BITSIZE bits.
 
    Extracting bits depends on endianness of the machine.  Compute the
    number of least significant bits to discard.  For big endian machines,
@@ -1978,23 +2140,30 @@ value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *ty
    If the field is signed, we also do sign extension. */
 
 LONGEST
-unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
+unpack_bits_as_long (struct type *field_type, const gdb_byte *valaddr,
+                    int bitpos, int bitsize)
 {
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
   ULONGEST val;
   ULONGEST valmask;
-  int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
-  int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
   int lsbcount;
-  struct type *field_type;
+  int bytes_read;
 
-  val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
-  field_type = TYPE_FIELD_TYPE (type, fieldno);
+  /* Read the minimum number of bytes required; there may not be
+     enough bytes to read an entire ULONGEST.  */
   CHECK_TYPEDEF (field_type);
+  if (bitsize)
+    bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
+  else
+    bytes_read = TYPE_LENGTH (field_type);
+
+  val = extract_unsigned_integer (valaddr + bitpos / 8,
+                                 bytes_read, byte_order);
 
   /* Extract bits.  See comment above. */
 
-  if (gdbarch_bits_big_endian (get_type_arch (type)))
-    lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
+  if (gdbarch_bits_big_endian (get_type_arch (field_type)))
+    lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
   else
     lsbcount = (bitpos % 8);
   val >>= lsbcount;
@@ -2017,6 +2186,19 @@ unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
   return (val);
 }
 
+/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
+   VALADDR.  See unpack_bits_as_long for more details.  */
+
+LONGEST
+unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
+{
+  int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
+  int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
+  struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
+
+  return unpack_bits_as_long (field_type, valaddr, bitpos, bitsize);
+}
+
 /* Modify the value of a bitfield.  ADDR points to a block of memory in
    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
@@ -2028,6 +2210,7 @@ void
 modify_field (struct type *type, gdb_byte *addr,
              LONGEST fieldval, int bitpos, int bitsize)
 {
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   ULONGEST oword;
   ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
 
@@ -2047,7 +2230,7 @@ modify_field (struct type *type, gdb_byte *addr,
       fieldval &= mask;
     }
 
-  oword = extract_unsigned_integer (addr, sizeof oword);
+  oword = extract_unsigned_integer (addr, sizeof oword, byte_order);
 
   /* Shifting for bit field depends on endianness of the target machine.  */
   if (gdbarch_bits_big_endian (get_type_arch (type)))
@@ -2056,7 +2239,7 @@ modify_field (struct type *type, gdb_byte *addr,
   oword &= ~(mask << bitpos);
   oword |= fieldval << bitpos;
 
-  store_unsigned_integer (addr, sizeof oword, oword);
+  store_unsigned_integer (addr, sizeof oword, byte_order, oword);
 }
 \f
 /* Pack NUM into BUF using a target format of TYPE.  */
@@ -2064,6 +2247,7 @@ modify_field (struct type *type, gdb_byte *addr,
 void
 pack_long (gdb_byte *buf, struct type *type, LONGEST num)
 {
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   int len;
 
   type = check_typedef (type);
@@ -2078,7 +2262,7 @@ pack_long (gdb_byte *buf, struct type *type, LONGEST num)
     case TYPE_CODE_BOOL:
     case TYPE_CODE_RANGE:
     case TYPE_CODE_MEMBERPTR:
-      store_signed_integer (buf, len, num);
+      store_signed_integer (buf, len, byte_order, num);
       break;
 
     case TYPE_CODE_REF:
@@ -2093,6 +2277,43 @@ pack_long (gdb_byte *buf, struct type *type, LONGEST num)
 }
 
 
+/* Pack NUM into BUF using a target format of TYPE.  */
+
+void
+pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
+{
+  int len;
+  enum bfd_endian byte_order;
+
+  type = check_typedef (type);
+  len = TYPE_LENGTH (type);
+  byte_order = gdbarch_byte_order (get_type_arch (type));
+
+  switch (TYPE_CODE (type))
+    {
+    case TYPE_CODE_INT:
+    case TYPE_CODE_CHAR:
+    case TYPE_CODE_ENUM:
+    case TYPE_CODE_FLAGS:
+    case TYPE_CODE_BOOL:
+    case TYPE_CODE_RANGE:
+    case TYPE_CODE_MEMBERPTR:
+      store_unsigned_integer (buf, len, byte_order, num);
+      break;
+
+    case TYPE_CODE_REF:
+    case TYPE_CODE_PTR:
+      store_typed_address (buf, type, (CORE_ADDR) num);
+      break;
+
+    default:
+      error (_("\
+Unexpected type (%d) encountered for unsigned integer constant."),
+            TYPE_CODE (type));
+    }
+}
+
+
 /* Convert C numbers into newly allocated values.  */
 
 struct value *
@@ -2101,6 +2322,18 @@ value_from_longest (struct type *type, LONGEST num)
   struct value *val = allocate_value (type);
 
   pack_long (value_contents_raw (val), type, num);
+  return val;
+}
+
+
+/* Convert C unsigned numbers into newly allocated values.  */
+
+struct value *
+value_from_ulongest (struct type *type, ULONGEST num)
+{
+  struct value *val = allocate_value (type);
+
+  pack_unsigned_long (value_contents_raw (val), type, num);
 
   return val;
 }
@@ -2112,7 +2345,8 @@ struct value *
 value_from_pointer (struct type *type, CORE_ADDR addr)
 {
   struct value *val = allocate_value (type);
-  store_typed_address (value_contents_raw (val), type, addr);
+
+  store_typed_address (value_contents_raw (val), check_typedef (type), addr);
   return val;
 }
 
@@ -2127,6 +2361,7 @@ value_from_contents_and_address (struct type *type,
                                 CORE_ADDR address)
 {
   struct value *v = allocate_value (type);
+
   if (valaddr == NULL)
     set_value_lazy (v, 1);
   else
@@ -2142,7 +2377,6 @@ value_from_double (struct type *type, DOUBLEST num)
   struct value *val = allocate_value (type);
   struct type *base_type = check_typedef (type);
   enum type_code code = TYPE_CODE (base_type);
-  int len = TYPE_LENGTH (base_type);
 
   if (code == TYPE_CODE_FLT)
     {
@@ -2160,7 +2394,6 @@ value_from_decfloat (struct type *type, const gdb_byte *dec)
   struct value *val = allocate_value (type);
 
   memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
-
   return val;
 }
 
@@ -2168,6 +2401,7 @@ struct value *
 coerce_ref (struct value *arg)
 {
   struct type *value_type_arg_tmp = check_typedef (value_type (arg));
+
   if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)
     arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
                         unpack_pointer (value_type (arg),              
@@ -2186,7 +2420,7 @@ coerce_array (struct value *arg)
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_ARRAY:
-      if (current_language->c_style_arrays)
+      if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
        arg = value_coerce_array (arg);
       break;
     case TYPE_CODE_FUNC:
This page took 0.056928 seconds and 4 git commands to generate.