119370Spst/* Low level packing and unpacking of values for GDB, the GNU Debugger.
2130803Smarcel
398944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130803Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003 Free Software
5130803Smarcel   Foundation, Inc.
619370Spst
798944Sobrien   This file is part of GDB.
819370Spst
998944Sobrien   This program is free software; you can redistribute it and/or modify
1098944Sobrien   it under the terms of the GNU General Public License as published by
1198944Sobrien   the Free Software Foundation; either version 2 of the License, or
1298944Sobrien   (at your option) any later version.
1319370Spst
1498944Sobrien   This program is distributed in the hope that it will be useful,
1598944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798944Sobrien   GNU General Public License for more details.
1819370Spst
1998944Sobrien   You should have received a copy of the GNU General Public License
2098944Sobrien   along with this program; if not, write to the Free Software
2198944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298944Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
2519370Spst#include "gdb_string.h"
2619370Spst#include "symtab.h"
2719370Spst#include "gdbtypes.h"
2819370Spst#include "value.h"
2919370Spst#include "gdbcore.h"
3019370Spst#include "command.h"
3119370Spst#include "gdbcmd.h"
3219370Spst#include "target.h"
3319370Spst#include "language.h"
3419370Spst#include "scm-lang.h"
3519370Spst#include "demangle.h"
3698944Sobrien#include "doublest.h"
3798944Sobrien#include "gdb_assert.h"
38130803Smarcel#include "regcache.h"
39130803Smarcel#include "block.h"
4019370Spst
4146283Sdfr/* Prototypes for exported functions. */
4219370Spst
4398944Sobrienvoid _initialize_values (void);
4446283Sdfr
4546283Sdfr/* Prototypes for local functions. */
4646283Sdfr
4798944Sobrienstatic void show_values (char *, int);
4819370Spst
4998944Sobrienstatic void show_convenience (char *, int);
5019370Spst
5146283Sdfr
5219370Spst/* The value-history records all the values printed
5319370Spst   by print commands during this session.  Each chunk
5419370Spst   records 60 consecutive values.  The first chunk on
5519370Spst   the chain records the most recent values.
5619370Spst   The total number of values is in value_history_count.  */
5719370Spst
5819370Spst#define VALUE_HISTORY_CHUNK 60
5919370Spst
6019370Spststruct value_history_chunk
6198944Sobrien  {
6298944Sobrien    struct value_history_chunk *next;
6398944Sobrien    struct value *values[VALUE_HISTORY_CHUNK];
6498944Sobrien  };
6519370Spst
6619370Spst/* Chain of chunks now in use.  */
6719370Spst
6819370Spststatic struct value_history_chunk *value_history_chain;
6919370Spst
7019370Spststatic int value_history_count;	/* Abs number of last entry stored */
7119370Spst
7219370Spst/* List of all value objects currently allocated
7319370Spst   (except for those released by calls to release_value)
7419370Spst   This is so they can be freed after each command.  */
7519370Spst
7698944Sobrienstatic struct value *all_values;
7719370Spst
7819370Spst/* Allocate a  value  that has the correct length for type TYPE.  */
7919370Spst
8098944Sobrienstruct value *
8198944Sobrienallocate_value (struct type *type)
8219370Spst{
8398944Sobrien  struct value *val;
8419370Spst  struct type *atype = check_typedef (type);
8519370Spst
8619370Spst  val = (struct value *) xmalloc (sizeof (struct value) + TYPE_LENGTH (atype));
8719370Spst  VALUE_NEXT (val) = all_values;
8819370Spst  all_values = val;
8919370Spst  VALUE_TYPE (val) = type;
9046283Sdfr  VALUE_ENCLOSING_TYPE (val) = type;
9119370Spst  VALUE_LVAL (val) = not_lval;
9219370Spst  VALUE_ADDRESS (val) = 0;
93130803Smarcel  VALUE_FRAME_ID (val) = null_frame_id;
9419370Spst  VALUE_OFFSET (val) = 0;
9519370Spst  VALUE_BITPOS (val) = 0;
9619370Spst  VALUE_BITSIZE (val) = 0;
9719370Spst  VALUE_REGNO (val) = -1;
9819370Spst  VALUE_LAZY (val) = 0;
9919370Spst  VALUE_OPTIMIZED_OUT (val) = 0;
10046283Sdfr  VALUE_BFD_SECTION (val) = NULL;
10146283Sdfr  VALUE_EMBEDDED_OFFSET (val) = 0;
10246283Sdfr  VALUE_POINTED_TO_OFFSET (val) = 0;
10319370Spst  val->modifiable = 1;
104283029Ssbruno  val->initialized = 1;  /* Default to initialized.  */
10519370Spst  return val;
10619370Spst}
10719370Spst
10819370Spst/* Allocate a  value  that has the correct length
10919370Spst   for COUNT repetitions type TYPE.  */
11019370Spst
11198944Sobrienstruct value *
11298944Sobrienallocate_repeat_value (struct type *type, int count)
11319370Spst{
11498944Sobrien  int low_bound = current_language->string_lower_bound;		/* ??? */
11519370Spst  /* FIXME-type-allocation: need a way to free this type when we are
11619370Spst     done with it.  */
11719370Spst  struct type *range_type
11898944Sobrien  = create_range_type ((struct type *) NULL, builtin_type_int,
11998944Sobrien		       low_bound, count + low_bound - 1);
12019370Spst  /* FIXME-type-allocation: need a way to free this type when we are
12119370Spst     done with it.  */
12219370Spst  return allocate_value (create_array_type ((struct type *) NULL,
12319370Spst					    type, range_type));
12419370Spst}
12519370Spst
12619370Spst/* Return a mark in the value chain.  All values allocated after the
12719370Spst   mark is obtained (except for those released) are subject to being freed
12819370Spst   if a subsequent value_free_to_mark is passed the mark.  */
12998944Sobrienstruct value *
13098944Sobrienvalue_mark (void)
13119370Spst{
13219370Spst  return all_values;
13319370Spst}
13419370Spst
13519370Spst/* Free all values allocated since MARK was obtained by value_mark
13619370Spst   (except for those released).  */
13719370Spstvoid
13898944Sobrienvalue_free_to_mark (struct value *mark)
13919370Spst{
14098944Sobrien  struct value *val;
14198944Sobrien  struct value *next;
14219370Spst
14319370Spst  for (val = all_values; val && val != mark; val = next)
14419370Spst    {
14519370Spst      next = VALUE_NEXT (val);
14619370Spst      value_free (val);
14719370Spst    }
14819370Spst  all_values = val;
14919370Spst}
15019370Spst
15119370Spst/* Free all the values that have been allocated (except for those released).
15219370Spst   Called after each command, successful or not.  */
15319370Spst
15419370Spstvoid
15598944Sobrienfree_all_values (void)
15619370Spst{
15798944Sobrien  struct value *val;
15898944Sobrien  struct value *next;
15919370Spst
16019370Spst  for (val = all_values; val; val = next)
16119370Spst    {
16219370Spst      next = VALUE_NEXT (val);
16319370Spst      value_free (val);
16419370Spst    }
16519370Spst
16619370Spst  all_values = 0;
16719370Spst}
16819370Spst
16919370Spst/* Remove VAL from the chain all_values
17019370Spst   so it will not be freed automatically.  */
17119370Spst
17219370Spstvoid
17398944Sobrienrelease_value (struct value *val)
17419370Spst{
17598944Sobrien  struct value *v;
17619370Spst
17719370Spst  if (all_values == val)
17819370Spst    {
17919370Spst      all_values = val->next;
18019370Spst      return;
18119370Spst    }
18219370Spst
18319370Spst  for (v = all_values; v; v = v->next)
18419370Spst    {
18519370Spst      if (v->next == val)
18619370Spst	{
18719370Spst	  v->next = val->next;
18819370Spst	  break;
18919370Spst	}
19019370Spst    }
19119370Spst}
19219370Spst
19319370Spst/* Release all values up to mark  */
19498944Sobrienstruct value *
19598944Sobrienvalue_release_to_mark (struct value *mark)
19619370Spst{
19798944Sobrien  struct value *val;
19898944Sobrien  struct value *next;
19919370Spst
20019370Spst  for (val = next = all_values; next; next = VALUE_NEXT (next))
20119370Spst    if (VALUE_NEXT (next) == mark)
20219370Spst      {
20319370Spst	all_values = VALUE_NEXT (next);
20419370Spst	VALUE_NEXT (next) = 0;
20519370Spst	return val;
20619370Spst      }
20719370Spst  all_values = 0;
20819370Spst  return val;
20919370Spst}
21019370Spst
21119370Spst/* Return a copy of the value ARG.
21219370Spst   It contains the same contents, for same memory address,
21319370Spst   but it's a different block of storage.  */
21419370Spst
21598944Sobrienstruct value *
21698944Sobrienvalue_copy (struct value *arg)
21719370Spst{
218130803Smarcel  struct type *encl_type = VALUE_ENCLOSING_TYPE (arg);
21998944Sobrien  struct value *val = allocate_value (encl_type);
22046283Sdfr  VALUE_TYPE (val) = VALUE_TYPE (arg);
22119370Spst  VALUE_LVAL (val) = VALUE_LVAL (arg);
22219370Spst  VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
22319370Spst  VALUE_OFFSET (val) = VALUE_OFFSET (arg);
22419370Spst  VALUE_BITPOS (val) = VALUE_BITPOS (arg);
22519370Spst  VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
226130803Smarcel  VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
22719370Spst  VALUE_REGNO (val) = VALUE_REGNO (arg);
22819370Spst  VALUE_LAZY (val) = VALUE_LAZY (arg);
22919370Spst  VALUE_OPTIMIZED_OUT (val) = VALUE_OPTIMIZED_OUT (arg);
23046283Sdfr  VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (arg);
23146283Sdfr  VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (arg);
23246283Sdfr  VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (arg);
23319370Spst  val->modifiable = arg->modifiable;
23419370Spst  if (!VALUE_LAZY (val))
23519370Spst    {
23646283Sdfr      memcpy (VALUE_CONTENTS_ALL_RAW (val), VALUE_CONTENTS_ALL_RAW (arg),
23746283Sdfr	      TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg)));
23846283Sdfr
23919370Spst    }
24019370Spst  return val;
24119370Spst}
24219370Spst
24319370Spst/* Access to the value history.  */
24419370Spst
24519370Spst/* Record a new value in the value history.
24619370Spst   Returns the absolute history index of the entry.
24719370Spst   Result of -1 indicates the value was not saved; otherwise it is the
24819370Spst   value history index of this new item.  */
24919370Spst
25019370Spstint
25198944Sobrienrecord_latest_value (struct value *val)
25219370Spst{
25319370Spst  int i;
25419370Spst
25519370Spst  /* We don't want this value to have anything to do with the inferior anymore.
25619370Spst     In particular, "set $1 = 50" should not affect the variable from which
25719370Spst     the value was taken, and fast watchpoints should be able to assume that
25819370Spst     a value on the value history never changes.  */
25919370Spst  if (VALUE_LAZY (val))
26019370Spst    value_fetch_lazy (val);
26119370Spst  /* We preserve VALUE_LVAL so that the user can find out where it was fetched
26219370Spst     from.  This is a bit dubious, because then *&$1 does not just return $1
26319370Spst     but the current contents of that location.  c'est la vie...  */
26419370Spst  val->modifiable = 0;
26519370Spst  release_value (val);
26619370Spst
26719370Spst  /* Here we treat value_history_count as origin-zero
26819370Spst     and applying to the value being stored now.  */
26919370Spst
27019370Spst  i = value_history_count % VALUE_HISTORY_CHUNK;
27119370Spst  if (i == 0)
27219370Spst    {
27398944Sobrien      struct value_history_chunk *new
27498944Sobrien      = (struct value_history_chunk *)
27598944Sobrien      xmalloc (sizeof (struct value_history_chunk));
27619370Spst      memset (new->values, 0, sizeof new->values);
27719370Spst      new->next = value_history_chain;
27819370Spst      value_history_chain = new;
27919370Spst    }
28019370Spst
28119370Spst  value_history_chain->values[i] = val;
28219370Spst
28319370Spst  /* Now we regard value_history_count as origin-one
28419370Spst     and applying to the value just stored.  */
28519370Spst
28619370Spst  return ++value_history_count;
28719370Spst}
28819370Spst
28919370Spst/* Return a copy of the value in the history with sequence number NUM.  */
29019370Spst
29198944Sobrienstruct value *
29298944Sobrienaccess_value_history (int num)
29319370Spst{
29498944Sobrien  struct value_history_chunk *chunk;
295130803Smarcel  int i;
296130803Smarcel  int absnum = num;
29719370Spst
29819370Spst  if (absnum <= 0)
29919370Spst    absnum += value_history_count;
30019370Spst
30119370Spst  if (absnum <= 0)
30219370Spst    {
30319370Spst      if (num == 0)
30419370Spst	error ("The history is empty.");
30519370Spst      else if (num == 1)
30619370Spst	error ("There is only one value in the history.");
30719370Spst      else
30819370Spst	error ("History does not go back to $$%d.", -num);
30919370Spst    }
31019370Spst  if (absnum > value_history_count)
31119370Spst    error ("History has not yet reached $%d.", absnum);
31219370Spst
31319370Spst  absnum--;
31419370Spst
31519370Spst  /* Now absnum is always absolute and origin zero.  */
31619370Spst
31719370Spst  chunk = value_history_chain;
31819370Spst  for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
31919370Spst       i > 0; i--)
32019370Spst    chunk = chunk->next;
32119370Spst
32219370Spst  return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
32319370Spst}
32419370Spst
32519370Spst/* Clear the value history entirely.
32619370Spst   Must be done when new symbol tables are loaded,
32719370Spst   because the type pointers become invalid.  */
32819370Spst
32919370Spstvoid
33098944Sobrienclear_value_history (void)
33119370Spst{
33298944Sobrien  struct value_history_chunk *next;
333130803Smarcel  int i;
33498944Sobrien  struct value *val;
33519370Spst
33619370Spst  while (value_history_chain)
33719370Spst    {
33819370Spst      for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
33919370Spst	if ((val = value_history_chain->values[i]) != NULL)
34098944Sobrien	  xfree (val);
34119370Spst      next = value_history_chain->next;
34298944Sobrien      xfree (value_history_chain);
34319370Spst      value_history_chain = next;
34419370Spst    }
34519370Spst  value_history_count = 0;
34619370Spst}
34719370Spst
34819370Spststatic void
34998944Sobrienshow_values (char *num_exp, int from_tty)
35019370Spst{
351130803Smarcel  int i;
35298944Sobrien  struct value *val;
35319370Spst  static int num = 1;
35419370Spst
35519370Spst  if (num_exp)
35619370Spst    {
35798944Sobrien      /* "info history +" should print from the stored position.
35898944Sobrien         "info history <exp>" should print around value number <exp>.  */
35919370Spst      if (num_exp[0] != '+' || num_exp[1] != '\0')
36098944Sobrien	num = parse_and_eval_long (num_exp) - 5;
36119370Spst    }
36219370Spst  else
36319370Spst    {
36419370Spst      /* "info history" means print the last 10 values.  */
36519370Spst      num = value_history_count - 9;
36619370Spst    }
36719370Spst
36819370Spst  if (num <= 0)
36919370Spst    num = 1;
37019370Spst
37119370Spst  for (i = num; i < num + 10 && i <= value_history_count; i++)
37219370Spst    {
37319370Spst      val = access_value_history (i);
37419370Spst      printf_filtered ("$%d = ", i);
37519370Spst      value_print (val, gdb_stdout, 0, Val_pretty_default);
37619370Spst      printf_filtered ("\n");
37719370Spst    }
37819370Spst
37919370Spst  /* The next "info history +" should start after what we just printed.  */
38019370Spst  num += 10;
38119370Spst
38219370Spst  /* Hitting just return after this command should do the same thing as
38319370Spst     "info history +".  If num_exp is null, this is unnecessary, since
38419370Spst     "info history +" is not useful after "info history".  */
38519370Spst  if (from_tty && num_exp)
38619370Spst    {
38719370Spst      num_exp[0] = '+';
38819370Spst      num_exp[1] = '\0';
38919370Spst    }
39019370Spst}
39119370Spst
39219370Spst/* Internal variables.  These are variables within the debugger
39319370Spst   that hold values assigned by debugger commands.
39419370Spst   The user refers to them with a '$' prefix
39519370Spst   that does not appear in the variable names stored internally.  */
39619370Spst
39719370Spststatic struct internalvar *internalvars;
39819370Spst
39919370Spst/* Look up an internal variable with name NAME.  NAME should not
40019370Spst   normally include a dollar sign.
40119370Spst
40219370Spst   If the specified internal variable does not exist,
40319370Spst   one is created, with a void value.  */
40419370Spst
40519370Spststruct internalvar *
40698944Sobrienlookup_internalvar (char *name)
40719370Spst{
408130803Smarcel  struct internalvar *var;
40919370Spst
41019370Spst  for (var = internalvars; var; var = var->next)
411130803Smarcel    if (strcmp (var->name, name) == 0)
41219370Spst      return var;
41319370Spst
41419370Spst  var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
41519370Spst  var->name = concat (name, NULL);
41619370Spst  var->value = allocate_value (builtin_type_void);
41719370Spst  release_value (var->value);
41819370Spst  var->next = internalvars;
41919370Spst  internalvars = var;
42019370Spst  return var;
42119370Spst}
42219370Spst
42398944Sobrienstruct value *
42498944Sobrienvalue_of_internalvar (struct internalvar *var)
42519370Spst{
42698944Sobrien  struct value *val;
42719370Spst
42819370Spst  val = value_copy (var->value);
42919370Spst  if (VALUE_LAZY (val))
43019370Spst    value_fetch_lazy (val);
43119370Spst  VALUE_LVAL (val) = lval_internalvar;
43219370Spst  VALUE_INTERNALVAR (val) = var;
43319370Spst  return val;
43419370Spst}
43519370Spst
43619370Spstvoid
43798944Sobrienset_internalvar_component (struct internalvar *var, int offset, int bitpos,
43898944Sobrien			   int bitsize, struct value *newval)
43919370Spst{
440130803Smarcel  char *addr = VALUE_CONTENTS (var->value) + offset;
44119370Spst
44219370Spst  if (bitsize)
44319370Spst    modify_field (addr, value_as_long (newval),
44419370Spst		  bitpos, bitsize);
44519370Spst  else
44619370Spst    memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval)));
44719370Spst}
44819370Spst
44919370Spstvoid
45098944Sobrienset_internalvar (struct internalvar *var, struct value *val)
45119370Spst{
45298944Sobrien  struct value *newval;
45319370Spst
45419370Spst  newval = value_copy (val);
45519370Spst  newval->modifiable = 1;
45619370Spst
45719370Spst  /* Force the value to be fetched from the target now, to avoid problems
45819370Spst     later when this internalvar is referenced and the target is gone or
45919370Spst     has changed.  */
46019370Spst  if (VALUE_LAZY (newval))
46119370Spst    value_fetch_lazy (newval);
46219370Spst
46319370Spst  /* Begin code which must not call error().  If var->value points to
46419370Spst     something free'd, an error() obviously leaves a dangling pointer.
46519370Spst     But we also get a danling pointer if var->value points to
46619370Spst     something in the value chain (i.e., before release_value is
46719370Spst     called), because after the error free_all_values will get called before
46819370Spst     long.  */
46998944Sobrien  xfree (var->value);
47019370Spst  var->value = newval;
47119370Spst  release_value (newval);
47219370Spst  /* End code which must not call error().  */
47319370Spst}
47419370Spst
47519370Spstchar *
47698944Sobrieninternalvar_name (struct internalvar *var)
47719370Spst{
47819370Spst  return var->name;
47919370Spst}
48019370Spst
48119370Spst/* Free all internalvars.  Done when new symtabs are loaded,
48219370Spst   because that makes the values invalid.  */
48319370Spst
48419370Spstvoid
48598944Sobrienclear_internalvars (void)
48619370Spst{
487130803Smarcel  struct internalvar *var;
48819370Spst
48919370Spst  while (internalvars)
49019370Spst    {
49119370Spst      var = internalvars;
49219370Spst      internalvars = var->next;
49398944Sobrien      xfree (var->name);
49498944Sobrien      xfree (var->value);
49598944Sobrien      xfree (var);
49619370Spst    }
49719370Spst}
49819370Spst
49919370Spststatic void
50098944Sobrienshow_convenience (char *ignore, int from_tty)
50119370Spst{
502130803Smarcel  struct internalvar *var;
50319370Spst  int varseen = 0;
50419370Spst
50519370Spst  for (var = internalvars; var; var = var->next)
50619370Spst    {
50719370Spst      if (!varseen)
50819370Spst	{
50919370Spst	  varseen = 1;
51019370Spst	}
51119370Spst      printf_filtered ("$%s = ", var->name);
51219370Spst      value_print (var->value, gdb_stdout, 0, Val_pretty_default);
51319370Spst      printf_filtered ("\n");
51419370Spst    }
51519370Spst  if (!varseen)
51619370Spst    printf_unfiltered ("No debugger convenience variables now defined.\n\
51719370SpstConvenience variables have names starting with \"$\";\n\
51819370Spstuse \"set\" as in \"set $foo = 5\" to define them.\n");
51919370Spst}
52019370Spst
52119370Spst/* Extract a value as a C number (either long or double).
52219370Spst   Knows how to convert fixed values to double, or
52319370Spst   floating values to long.
52419370Spst   Does not deallocate the value.  */
52519370Spst
52619370SpstLONGEST
52798944Sobrienvalue_as_long (struct value *val)
52819370Spst{
52919370Spst  /* This coerces arrays and functions, which is necessary (e.g.
53019370Spst     in disassemble_command).  It also dereferences references, which
53119370Spst     I suspect is the most logical thing to do.  */
53219370Spst  COERCE_ARRAY (val);
53319370Spst  return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
53419370Spst}
53519370Spst
53619370SpstDOUBLEST
53798944Sobrienvalue_as_double (struct value *val)
53819370Spst{
53919370Spst  DOUBLEST foo;
54019370Spst  int inv;
54198944Sobrien
54219370Spst  foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
54319370Spst  if (inv)
54419370Spst    error ("Invalid floating value found in program.");
54519370Spst  return foo;
54619370Spst}
54798944Sobrien/* Extract a value as a C pointer. Does not deallocate the value.
54898944Sobrien   Note that val's type may not actually be a pointer; value_as_long
54998944Sobrien   handles all the cases.  */
55019370SpstCORE_ADDR
55198944Sobrienvalue_as_address (struct value *val)
55219370Spst{
55319370Spst  /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
55419370Spst     whether we want this to be true eventually.  */
55519370Spst#if 0
55619370Spst  /* ADDR_BITS_REMOVE is wrong if we are being called for a
55719370Spst     non-address (e.g. argument to "signal", "info break", etc.), or
55819370Spst     for pointers to char, in which the low bits *are* significant.  */
55998944Sobrien  return ADDR_BITS_REMOVE (value_as_long (val));
56019370Spst#else
56198944Sobrien
56298944Sobrien  /* There are several targets (IA-64, PowerPC, and others) which
56398944Sobrien     don't represent pointers to functions as simply the address of
56498944Sobrien     the function's entry point.  For example, on the IA-64, a
56598944Sobrien     function pointer points to a two-word descriptor, generated by
56698944Sobrien     the linker, which contains the function's entry point, and the
56798944Sobrien     value the IA-64 "global pointer" register should have --- to
56898944Sobrien     support position-independent code.  The linker generates
56998944Sobrien     descriptors only for those functions whose addresses are taken.
57098944Sobrien
57198944Sobrien     On such targets, it's difficult for GDB to convert an arbitrary
57298944Sobrien     function address into a function pointer; it has to either find
57398944Sobrien     an existing descriptor for that function, or call malloc and
57498944Sobrien     build its own.  On some targets, it is impossible for GDB to
57598944Sobrien     build a descriptor at all: the descriptor must contain a jump
57698944Sobrien     instruction; data memory cannot be executed; and code memory
57798944Sobrien     cannot be modified.
57898944Sobrien
57998944Sobrien     Upon entry to this function, if VAL is a value of type `function'
58098944Sobrien     (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
58198944Sobrien     VALUE_ADDRESS (val) is the address of the function.  This is what
58298944Sobrien     you'll get if you evaluate an expression like `main'.  The call
58398944Sobrien     to COERCE_ARRAY below actually does all the usual unary
58498944Sobrien     conversions, which includes converting values of type `function'
58598944Sobrien     to `pointer to function'.  This is the challenging conversion
58698944Sobrien     discussed above.  Then, `unpack_long' will convert that pointer
58798944Sobrien     back into an address.
58898944Sobrien
58998944Sobrien     So, suppose the user types `disassemble foo' on an architecture
59098944Sobrien     with a strange function pointer representation, on which GDB
59198944Sobrien     cannot build its own descriptors, and suppose further that `foo'
59298944Sobrien     has no linker-built descriptor.  The address->pointer conversion
59398944Sobrien     will signal an error and prevent the command from running, even
59498944Sobrien     though the next step would have been to convert the pointer
59598944Sobrien     directly back into the same address.
59698944Sobrien
59798944Sobrien     The following shortcut avoids this whole mess.  If VAL is a
59898944Sobrien     function, just return its address directly.  */
59998944Sobrien  if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
60098944Sobrien      || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_METHOD)
60198944Sobrien    return VALUE_ADDRESS (val);
60298944Sobrien
60398944Sobrien  COERCE_ARRAY (val);
60498944Sobrien
60598944Sobrien  /* Some architectures (e.g. Harvard), map instruction and data
60698944Sobrien     addresses onto a single large unified address space.  For
60798944Sobrien     instance: An architecture may consider a large integer in the
60898944Sobrien     range 0x10000000 .. 0x1000ffff to already represent a data
60998944Sobrien     addresses (hence not need a pointer to address conversion) while
61098944Sobrien     a small integer would still need to be converted integer to
61198944Sobrien     pointer to address.  Just assume such architectures handle all
61298944Sobrien     integer conversions in a single function.  */
61398944Sobrien
61498944Sobrien  /* JimB writes:
61598944Sobrien
61698944Sobrien     I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
61798944Sobrien     must admonish GDB hackers to make sure its behavior matches the
61898944Sobrien     compiler's, whenever possible.
61998944Sobrien
62098944Sobrien     In general, I think GDB should evaluate expressions the same way
62198944Sobrien     the compiler does.  When the user copies an expression out of
62298944Sobrien     their source code and hands it to a `print' command, they should
62398944Sobrien     get the same value the compiler would have computed.  Any
62498944Sobrien     deviation from this rule can cause major confusion and annoyance,
62598944Sobrien     and needs to be justified carefully.  In other words, GDB doesn't
62698944Sobrien     really have the freedom to do these conversions in clever and
62798944Sobrien     useful ways.
62898944Sobrien
62998944Sobrien     AndrewC pointed out that users aren't complaining about how GDB
63098944Sobrien     casts integers to pointers; they are complaining that they can't
63198944Sobrien     take an address from a disassembly listing and give it to `x/i'.
63298944Sobrien     This is certainly important.
63398944Sobrien
63498944Sobrien     Adding an architecture method like INTEGER_TO_ADDRESS certainly
63598944Sobrien     makes it possible for GDB to "get it right" in all circumstances
63698944Sobrien     --- the target has complete control over how things get done, so
63798944Sobrien     people can Do The Right Thing for their target without breaking
63898944Sobrien     anyone else.  The standard doesn't specify how integers get
63998944Sobrien     converted to pointers; usually, the ABI doesn't either, but
64098944Sobrien     ABI-specific code is a more reasonable place to handle it.  */
64198944Sobrien
64298944Sobrien  if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_PTR
64398944Sobrien      && TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_REF
64498944Sobrien      && INTEGER_TO_ADDRESS_P ())
64598944Sobrien    return INTEGER_TO_ADDRESS (VALUE_TYPE (val), VALUE_CONTENTS (val));
64698944Sobrien
64798944Sobrien  return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
64819370Spst#endif
64919370Spst}
65019370Spst
65119370Spst/* Unpack raw data (copied from debugee, target byte order) at VALADDR
65219370Spst   as a long, or as a double, assuming the raw data is described
65319370Spst   by type TYPE.  Knows how to convert different sizes of values
65419370Spst   and can convert between fixed and floating point.  We don't assume
65519370Spst   any alignment for the raw data.  Return value is in host byte order.
65619370Spst
65719370Spst   If you want functions and arrays to be coerced to pointers, and
65819370Spst   references to be dereferenced, call value_as_long() instead.
65919370Spst
66019370Spst   C++: It is assumed that the front-end has taken care of
66119370Spst   all matters concerning pointers to members.  A pointer
66219370Spst   to member which reaches here is considered to be equivalent
66319370Spst   to an INT (or some size).  After all, it is only an offset.  */
66419370Spst
66519370SpstLONGEST
666130803Smarcelunpack_long (struct type *type, const char *valaddr)
66719370Spst{
668130803Smarcel  enum type_code code = TYPE_CODE (type);
669130803Smarcel  int len = TYPE_LENGTH (type);
670130803Smarcel  int nosign = TYPE_UNSIGNED (type);
67119370Spst
67219370Spst  if (current_language->la_language == language_scm
67319370Spst      && is_scmvalue_type (type))
67419370Spst    return scm_unpack (type, valaddr, TYPE_CODE_INT);
67519370Spst
67619370Spst  switch (code)
67719370Spst    {
67819370Spst    case TYPE_CODE_TYPEDEF:
67919370Spst      return unpack_long (check_typedef (type), valaddr);
68019370Spst    case TYPE_CODE_ENUM:
68119370Spst    case TYPE_CODE_BOOL:
68219370Spst    case TYPE_CODE_INT:
68319370Spst    case TYPE_CODE_CHAR:
68419370Spst    case TYPE_CODE_RANGE:
68519370Spst      if (nosign)
68619370Spst	return extract_unsigned_integer (valaddr, len);
68719370Spst      else
68819370Spst	return extract_signed_integer (valaddr, len);
68919370Spst
69019370Spst    case TYPE_CODE_FLT:
69198944Sobrien      return extract_typed_floating (valaddr, type);
69219370Spst
69319370Spst    case TYPE_CODE_PTR:
69419370Spst    case TYPE_CODE_REF:
69519370Spst      /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
69698944Sobrien         whether we want this to be true eventually.  */
69798944Sobrien      return extract_typed_address (valaddr, type);
69819370Spst
69919370Spst    case TYPE_CODE_MEMBER:
70019370Spst      error ("not implemented: member types in unpack_long");
70119370Spst
70219370Spst    default:
70319370Spst      error ("Value can't be converted to integer.");
70419370Spst    }
70598944Sobrien  return 0;			/* Placate lint.  */
70619370Spst}
70719370Spst
70819370Spst/* Return a double value from the specified type and address.
70919370Spst   INVP points to an int which is set to 0 for valid value,
71019370Spst   1 for invalid value (bad float format).  In either case,
71119370Spst   the returned double is OK to use.  Argument is in target
71219370Spst   format, result is in host format.  */
71319370Spst
71419370SpstDOUBLEST
715130803Smarcelunpack_double (struct type *type, const char *valaddr, int *invp)
71619370Spst{
71746283Sdfr  enum type_code code;
71846283Sdfr  int len;
71946283Sdfr  int nosign;
72019370Spst
72119370Spst  *invp = 0;			/* Assume valid.   */
72219370Spst  CHECK_TYPEDEF (type);
72346283Sdfr  code = TYPE_CODE (type);
72446283Sdfr  len = TYPE_LENGTH (type);
72546283Sdfr  nosign = TYPE_UNSIGNED (type);
72619370Spst  if (code == TYPE_CODE_FLT)
72719370Spst    {
72898944Sobrien      /* NOTE: cagney/2002-02-19: There was a test here to see if the
72998944Sobrien	 floating-point value was valid (using the macro
73098944Sobrien	 INVALID_FLOAT).  That test/macro have been removed.
73198944Sobrien
73298944Sobrien	 It turns out that only the VAX defined this macro and then
73398944Sobrien	 only in a non-portable way.  Fixing the portability problem
73498944Sobrien	 wouldn't help since the VAX floating-point code is also badly
73598944Sobrien	 bit-rotten.  The target needs to add definitions for the
73698944Sobrien	 methods TARGET_FLOAT_FORMAT and TARGET_DOUBLE_FORMAT - these
73798944Sobrien	 exactly describe the target floating-point format.  The
73898944Sobrien	 problem here is that the corresponding floatformat_vax_f and
73998944Sobrien	 floatformat_vax_d values these methods should be set to are
74098944Sobrien	 also not defined either.  Oops!
74198944Sobrien
74298944Sobrien         Hopefully someone will add both the missing floatformat
743130803Smarcel         definitions and the new cases for floatformat_is_valid ().  */
744130803Smarcel
745130803Smarcel      if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
746130803Smarcel	{
747130803Smarcel	  *invp = 1;
748130803Smarcel	  return 0.0;
749130803Smarcel	}
750130803Smarcel
75198944Sobrien      return extract_typed_floating (valaddr, type);
75219370Spst    }
75319370Spst  else if (nosign)
75419370Spst    {
75519370Spst      /* Unsigned -- be sure we compensate for signed LONGEST.  */
75646283Sdfr      return (ULONGEST) unpack_long (type, valaddr);
75719370Spst    }
75819370Spst  else
75919370Spst    {
76019370Spst      /* Signed -- we are OK with unpack_long.  */
76119370Spst      return unpack_long (type, valaddr);
76219370Spst    }
76319370Spst}
76419370Spst
76519370Spst/* Unpack raw data (copied from debugee, target byte order) at VALADDR
76619370Spst   as a CORE_ADDR, assuming the raw data is described by type TYPE.
76719370Spst   We don't assume any alignment for the raw data.  Return value is in
76819370Spst   host byte order.
76919370Spst
77019370Spst   If you want functions and arrays to be coerced to pointers, and
77198944Sobrien   references to be dereferenced, call value_as_address() instead.
77219370Spst
77319370Spst   C++: It is assumed that the front-end has taken care of
77419370Spst   all matters concerning pointers to members.  A pointer
77519370Spst   to member which reaches here is considered to be equivalent
77619370Spst   to an INT (or some size).  After all, it is only an offset.  */
77719370Spst
77819370SpstCORE_ADDR
779130803Smarcelunpack_pointer (struct type *type, const char *valaddr)
78019370Spst{
78119370Spst  /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
78219370Spst     whether we want this to be true eventually.  */
78319370Spst  return unpack_long (type, valaddr);
78419370Spst}
78598944Sobrien
78619370Spst
787130803Smarcel/* Get the value of the FIELDN'th field (which must be static) of
788130803Smarcel   TYPE.  Return NULL if the field doesn't exist or has been
789130803Smarcel   optimized out. */
79046283Sdfr
79198944Sobrienstruct value *
79298944Sobrienvalue_static_field (struct type *type, int fieldno)
79346283Sdfr{
794130803Smarcel  struct value *retval;
795130803Smarcel
79646283Sdfr  if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno))
79746283Sdfr    {
798130803Smarcel      retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
799130803Smarcel			 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno),
800130803Smarcel			 NULL);
80146283Sdfr    }
80246283Sdfr  else
80346283Sdfr    {
80446283Sdfr      char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
805130803Smarcel      struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0, NULL);
80646283Sdfr      if (sym == NULL)
80746283Sdfr	{
80846283Sdfr	  /* With some compilers, e.g. HP aCC, static data members are reported
80998944Sobrien	     as non-debuggable symbols */
81098944Sobrien	  struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
81146283Sdfr	  if (!msym)
81246283Sdfr	    return NULL;
81346283Sdfr	  else
81498944Sobrien	    {
815130803Smarcel	      retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
816130803Smarcel				 SYMBOL_VALUE_ADDRESS (msym),
817130803Smarcel				 SYMBOL_BFD_SECTION (msym));
81846283Sdfr	    }
81946283Sdfr	}
82046283Sdfr      else
82146283Sdfr	{
822130803Smarcel	  /* SYM should never have a SYMBOL_CLASS which will require
823130803Smarcel	     read_var_value to use the FRAME parameter.  */
824130803Smarcel	  if (symbol_read_needs_frame (sym))
825130803Smarcel	    warning ("static field's value depends on the current "
826130803Smarcel		     "frame - bad debug info?");
827130803Smarcel	  retval = read_var_value (sym, NULL);
82898944Sobrien 	}
829130803Smarcel      if (retval && VALUE_LVAL (retval) == lval_memory)
830130803Smarcel	SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
831130803Smarcel			    VALUE_ADDRESS (retval));
83246283Sdfr    }
833130803Smarcel  return retval;
83446283Sdfr}
83546283Sdfr
83698944Sobrien/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
83798944Sobrien   You have to be careful here, since the size of the data area for the value
83898944Sobrien   is set by the length of the enclosing type.  So if NEW_ENCL_TYPE is bigger
83998944Sobrien   than the old enclosing type, you have to allocate more space for the data.
84098944Sobrien   The return value is a pointer to the new version of this value structure. */
84198944Sobrien
84298944Sobrienstruct value *
84398944Sobrienvalue_change_enclosing_type (struct value *val, struct type *new_encl_type)
84498944Sobrien{
84598944Sobrien  if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)))
84698944Sobrien    {
84798944Sobrien      VALUE_ENCLOSING_TYPE (val) = new_encl_type;
84898944Sobrien      return val;
84998944Sobrien    }
85098944Sobrien  else
85198944Sobrien    {
85298944Sobrien      struct value *new_val;
85398944Sobrien      struct value *prev;
85498944Sobrien
85598944Sobrien      new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type));
856130803Smarcel
857130803Smarcel      VALUE_ENCLOSING_TYPE (new_val) = new_encl_type;
858130803Smarcel
85998944Sobrien      /* We have to make sure this ends up in the same place in the value
86098944Sobrien	 chain as the original copy, so it's clean-up behavior is the same.
86198944Sobrien	 If the value has been released, this is a waste of time, but there
86298944Sobrien	 is no way to tell that in advance, so... */
86398944Sobrien
86498944Sobrien      if (val != all_values)
86598944Sobrien	{
86698944Sobrien	  for (prev = all_values; prev != NULL; prev = prev->next)
86798944Sobrien	    {
86898944Sobrien	      if (prev->next == val)
86998944Sobrien		{
87098944Sobrien		  prev->next = new_val;
87198944Sobrien		  break;
87298944Sobrien		}
87398944Sobrien	    }
87498944Sobrien	}
87598944Sobrien
87698944Sobrien      return new_val;
87798944Sobrien    }
87898944Sobrien}
87998944Sobrien
88019370Spst/* Given a value ARG1 (offset by OFFSET bytes)
88119370Spst   of a struct or union type ARG_TYPE,
88246283Sdfr   extract and return the value of one of its (non-static) fields.
88346283Sdfr   FIELDNO says which field. */
88419370Spst
88598944Sobrienstruct value *
88698944Sobrienvalue_primitive_field (struct value *arg1, int offset,
887130803Smarcel		       int fieldno, struct type *arg_type)
88819370Spst{
88998944Sobrien  struct value *v;
890130803Smarcel  struct type *type;
89119370Spst
89219370Spst  CHECK_TYPEDEF (arg_type);
89319370Spst  type = TYPE_FIELD_TYPE (arg_type, fieldno);
89419370Spst
89519370Spst  /* Handle packed fields */
89619370Spst
89719370Spst  if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
89819370Spst    {
89919370Spst      v = value_from_longest (type,
90046283Sdfr			      unpack_field_as_long (arg_type,
90146283Sdfr						    VALUE_CONTENTS (arg1)
90298944Sobrien						    + offset,
90346283Sdfr						    fieldno));
90419370Spst      VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
90519370Spst      VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
90698944Sobrien      VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
90798944Sobrien	+ TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
90819370Spst    }
90946283Sdfr  else if (fieldno < TYPE_N_BASECLASSES (arg_type))
91046283Sdfr    {
91146283Sdfr      /* This field is actually a base subobject, so preserve the
91246283Sdfr         entire object's contents for later references to virtual
91346283Sdfr         bases, etc.  */
91446283Sdfr      v = allocate_value (VALUE_ENCLOSING_TYPE (arg1));
91598944Sobrien      VALUE_TYPE (v) = type;
91646283Sdfr      if (VALUE_LAZY (arg1))
91746283Sdfr	VALUE_LAZY (v) = 1;
91846283Sdfr      else
91946283Sdfr	memcpy (VALUE_CONTENTS_ALL_RAW (v), VALUE_CONTENTS_ALL_RAW (arg1),
92046283Sdfr		TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg1)));
92146283Sdfr      VALUE_OFFSET (v) = VALUE_OFFSET (arg1);
92246283Sdfr      VALUE_EMBEDDED_OFFSET (v)
92398944Sobrien	= offset +
92498944Sobrien	VALUE_EMBEDDED_OFFSET (arg1) +
92598944Sobrien	TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
92646283Sdfr    }
92719370Spst  else
92819370Spst    {
92946283Sdfr      /* Plain old data member */
93046283Sdfr      offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
93119370Spst      v = allocate_value (type);
93219370Spst      if (VALUE_LAZY (arg1))
93319370Spst	VALUE_LAZY (v) = 1;
93419370Spst      else
93546283Sdfr	memcpy (VALUE_CONTENTS_RAW (v),
93646283Sdfr		VALUE_CONTENTS_RAW (arg1) + offset,
93719370Spst		TYPE_LENGTH (type));
93898944Sobrien      VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
93998944Sobrien			 + VALUE_EMBEDDED_OFFSET (arg1);
94019370Spst    }
94119370Spst  VALUE_LVAL (v) = VALUE_LVAL (arg1);
94219370Spst  if (VALUE_LVAL (arg1) == lval_internalvar)
94319370Spst    VALUE_LVAL (v) = lval_internalvar_component;
94419370Spst  VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
94598944Sobrien  VALUE_REGNO (v) = VALUE_REGNO (arg1);
94646283Sdfr/*  VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
94798944Sobrien   + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; */
94819370Spst  return v;
94919370Spst}
95019370Spst
95119370Spst/* Given a value ARG1 of a struct or union type,
95246283Sdfr   extract and return the value of one of its (non-static) fields.
95346283Sdfr   FIELDNO says which field. */
95419370Spst
95598944Sobrienstruct value *
956130803Smarcelvalue_field (struct value *arg1, int fieldno)
95719370Spst{
95819370Spst  return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
95919370Spst}
96019370Spst
96119370Spst/* Return a non-virtual function as a value.
96219370Spst   F is the list of member functions which contains the desired method.
96398944Sobrien   J is an index into F which provides the desired method.
96419370Spst
96598944Sobrien   We only use the symbol for its address, so be happy with either a
96698944Sobrien   full symbol or a minimal symbol.
96798944Sobrien */
96898944Sobrien
96998944Sobrienstruct value *
97098944Sobrienvalue_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
97198944Sobrien		int offset)
97219370Spst{
97398944Sobrien  struct value *v;
974130803Smarcel  struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
97598944Sobrien  char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
97619370Spst  struct symbol *sym;
97798944Sobrien  struct minimal_symbol *msym;
97819370Spst
979130803Smarcel  sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0, NULL);
98098944Sobrien  if (sym != NULL)
98198944Sobrien    {
98298944Sobrien      msym = NULL;
98398944Sobrien    }
98498944Sobrien  else
98598944Sobrien    {
98698944Sobrien      gdb_assert (sym == NULL);
98798944Sobrien      msym = lookup_minimal_symbol (physname, NULL, NULL);
98898944Sobrien      if (msym == NULL)
98919370Spst	return NULL;
99098944Sobrien    }
99198944Sobrien
99219370Spst  v = allocate_value (ftype);
99398944Sobrien  if (sym)
99498944Sobrien    {
99598944Sobrien      VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
99698944Sobrien    }
99798944Sobrien  else
99898944Sobrien    {
99998944Sobrien      VALUE_ADDRESS (v) = SYMBOL_VALUE_ADDRESS (msym);
100098944Sobrien    }
100119370Spst
100219370Spst  if (arg1p)
100398944Sobrien    {
100498944Sobrien      if (type != VALUE_TYPE (*arg1p))
100598944Sobrien	*arg1p = value_ind (value_cast (lookup_pointer_type (type),
100698944Sobrien					value_addr (*arg1p)));
100719370Spst
100898944Sobrien      /* Move the `this' pointer according to the offset.
100998944Sobrien         VALUE_OFFSET (*arg1p) += offset;
101098944Sobrien       */
101119370Spst    }
101219370Spst
101319370Spst  return v;
101419370Spst}
101519370Spst
101619370Spst
101719370Spst/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
101819370Spst   VALADDR.
101919370Spst
102019370Spst   Extracting bits depends on endianness of the machine.  Compute the
102119370Spst   number of least significant bits to discard.  For big endian machines,
102219370Spst   we compute the total number of bits in the anonymous object, subtract
102319370Spst   off the bit count from the MSB of the object to the MSB of the
102419370Spst   bitfield, then the size of the bitfield, which leaves the LSB discard
102519370Spst   count.  For little endian machines, the discard count is simply the
102619370Spst   number of bits from the LSB of the anonymous object to the LSB of the
102719370Spst   bitfield.
102819370Spst
102919370Spst   If the field is signed, we also do sign extension. */
103019370Spst
103119370SpstLONGEST
1032130803Smarcelunpack_field_as_long (struct type *type, const char *valaddr, int fieldno)
103319370Spst{
103446283Sdfr  ULONGEST val;
103546283Sdfr  ULONGEST valmask;
103619370Spst  int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
103719370Spst  int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
103819370Spst  int lsbcount;
103946283Sdfr  struct type *field_type;
104019370Spst
104119370Spst  val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
104246283Sdfr  field_type = TYPE_FIELD_TYPE (type, fieldno);
104346283Sdfr  CHECK_TYPEDEF (field_type);
104419370Spst
104519370Spst  /* Extract bits.  See comment above. */
104619370Spst
104719370Spst  if (BITS_BIG_ENDIAN)
104819370Spst    lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
104919370Spst  else
105019370Spst    lsbcount = (bitpos % 8);
105119370Spst  val >>= lsbcount;
105219370Spst
105319370Spst  /* If the field does not entirely fill a LONGEST, then zero the sign bits.
105419370Spst     If the field is signed, and is negative, then sign extend. */
105519370Spst
105619370Spst  if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
105719370Spst    {
105846283Sdfr      valmask = (((ULONGEST) 1) << bitsize) - 1;
105919370Spst      val &= valmask;
106046283Sdfr      if (!TYPE_UNSIGNED (field_type))
106119370Spst	{
106219370Spst	  if (val & (valmask ^ (valmask >> 1)))
106319370Spst	    {
106419370Spst	      val |= ~valmask;
106519370Spst	    }
106619370Spst	}
106719370Spst    }
106819370Spst  return (val);
106919370Spst}
107019370Spst
107119370Spst/* Modify the value of a bitfield.  ADDR points to a block of memory in
107219370Spst   target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
107319370Spst   is the desired value of the field, in host byte order.  BITPOS and BITSIZE
107419370Spst   indicate which bits (in target bit order) comprise the bitfield.  */
107519370Spst
107619370Spstvoid
107798944Sobrienmodify_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
107819370Spst{
107919370Spst  LONGEST oword;
108019370Spst
108119370Spst  /* If a negative fieldval fits in the field in question, chop
108219370Spst     off the sign extension bits.  */
108319370Spst  if (bitsize < (8 * (int) sizeof (fieldval))
108419370Spst      && (~fieldval & ~((1 << (bitsize - 1)) - 1)) == 0)
108519370Spst    fieldval = fieldval & ((1 << bitsize) - 1);
108619370Spst
108719370Spst  /* Warn if value is too big to fit in the field in question.  */
108819370Spst  if (bitsize < (8 * (int) sizeof (fieldval))
108998944Sobrien      && 0 != (fieldval & ~((1 << bitsize) - 1)))
109019370Spst    {
109119370Spst      /* FIXME: would like to include fieldval in the message, but
109298944Sobrien         we don't have a sprintf_longest.  */
109319370Spst      warning ("Value does not fit in %d bits.", bitsize);
109419370Spst
109519370Spst      /* Truncate it, otherwise adjoining fields may be corrupted.  */
109619370Spst      fieldval = fieldval & ((1 << bitsize) - 1);
109719370Spst    }
109819370Spst
109919370Spst  oword = extract_signed_integer (addr, sizeof oword);
110019370Spst
110119370Spst  /* Shifting for bit field depends on endianness of the target machine.  */
110219370Spst  if (BITS_BIG_ENDIAN)
110319370Spst    bitpos = sizeof (oword) * 8 - bitpos - bitsize;
110419370Spst
110519370Spst  /* Mask out old value, while avoiding shifts >= size of oword */
110619370Spst  if (bitsize < 8 * (int) sizeof (oword))
110798944Sobrien    oword &= ~(((((ULONGEST) 1) << bitsize) - 1) << bitpos);
110819370Spst  else
110998944Sobrien    oword &= ~((~(ULONGEST) 0) << bitpos);
111019370Spst  oword |= fieldval << bitpos;
111119370Spst
111219370Spst  store_signed_integer (addr, sizeof oword, oword);
111319370Spst}
111419370Spst
111519370Spst/* Convert C numbers into newly allocated values */
111619370Spst
111798944Sobrienstruct value *
1118130803Smarcelvalue_from_longest (struct type *type, LONGEST num)
111919370Spst{
112098944Sobrien  struct value *val = allocate_value (type);
1121130803Smarcel  enum type_code code;
1122130803Smarcel  int len;
112398944Sobrienretry:
112419370Spst  code = TYPE_CODE (type);
112519370Spst  len = TYPE_LENGTH (type);
112619370Spst
112719370Spst  switch (code)
112819370Spst    {
112919370Spst    case TYPE_CODE_TYPEDEF:
113019370Spst      type = check_typedef (type);
113119370Spst      goto retry;
113219370Spst    case TYPE_CODE_INT:
113319370Spst    case TYPE_CODE_CHAR:
113419370Spst    case TYPE_CODE_ENUM:
113519370Spst    case TYPE_CODE_BOOL:
113619370Spst    case TYPE_CODE_RANGE:
113719370Spst      store_signed_integer (VALUE_CONTENTS_RAW (val), len, num);
113819370Spst      break;
113998944Sobrien
114019370Spst    case TYPE_CODE_REF:
114119370Spst    case TYPE_CODE_PTR:
114298944Sobrien      store_typed_address (VALUE_CONTENTS_RAW (val), type, (CORE_ADDR) num);
114319370Spst      break;
114498944Sobrien
114519370Spst    default:
114646283Sdfr      error ("Unexpected type (%d) encountered for integer constant.", code);
114719370Spst    }
114819370Spst  return val;
114919370Spst}
115019370Spst
115198944Sobrien
115298944Sobrien/* Create a value representing a pointer of type TYPE to the address
115398944Sobrien   ADDR.  */
115498944Sobrienstruct value *
115598944Sobrienvalue_from_pointer (struct type *type, CORE_ADDR addr)
115619370Spst{
115798944Sobrien  struct value *val = allocate_value (type);
115898944Sobrien  store_typed_address (VALUE_CONTENTS_RAW (val), type, addr);
115998944Sobrien  return val;
116098944Sobrien}
116198944Sobrien
116298944Sobrien
116398944Sobrien/* Create a value for a string constant to be stored locally
116498944Sobrien   (not in the inferior's memory space, but in GDB memory).
116598944Sobrien   This is analogous to value_from_longest, which also does not
116698944Sobrien   use inferior memory.  String shall NOT contain embedded nulls.  */
116798944Sobrien
116898944Sobrienstruct value *
116998944Sobrienvalue_from_string (char *ptr)
117098944Sobrien{
117198944Sobrien  struct value *val;
117298944Sobrien  int len = strlen (ptr);
117398944Sobrien  int lowbound = current_language->string_lower_bound;
117498944Sobrien  struct type *rangetype =
117598944Sobrien  create_range_type ((struct type *) NULL,
117698944Sobrien		     builtin_type_int,
117798944Sobrien		     lowbound, len + lowbound - 1);
117898944Sobrien  struct type *stringtype =
117998944Sobrien  create_array_type ((struct type *) NULL,
118098944Sobrien		     *current_language->string_char_type,
118198944Sobrien		     rangetype);
118298944Sobrien
118398944Sobrien  val = allocate_value (stringtype);
118498944Sobrien  memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
118598944Sobrien  return val;
118698944Sobrien}
118798944Sobrien
118898944Sobrienstruct value *
118998944Sobrienvalue_from_double (struct type *type, DOUBLEST num)
119098944Sobrien{
119198944Sobrien  struct value *val = allocate_value (type);
119219370Spst  struct type *base_type = check_typedef (type);
1193130803Smarcel  enum type_code code = TYPE_CODE (base_type);
1194130803Smarcel  int len = TYPE_LENGTH (base_type);
119519370Spst
119619370Spst  if (code == TYPE_CODE_FLT)
119719370Spst    {
119898944Sobrien      store_typed_floating (VALUE_CONTENTS_RAW (val), base_type, num);
119919370Spst    }
120019370Spst  else
120119370Spst    error ("Unexpected type encountered for floating constant.");
120219370Spst
120319370Spst  return val;
120419370Spst}
120519370Spst
1206130803Smarcel/* Deal with the return-value of a function that has "just returned".
120719370Spst
1208130803Smarcel   Extract the return-value (as a "struct value") that a function,
1209130803Smarcel   using register convention, has just returned to its caller.  Assume
1210130803Smarcel   that the type of the function is VALTYPE, and that the "just
1211130803Smarcel   returned" register state is found in RETBUF.
121219370Spst
1213130803Smarcel   The function has "just returned" because GDB halts a returning
1214130803Smarcel   function by setting a breakpoint at the return address (in the
1215130803Smarcel   caller), and not the return instruction (in the callee).
1216130803Smarcel
1217130803Smarcel   Because, in the case of a return from an inferior function call,
1218130803Smarcel   GDB needs to restore the inferiors registers, RETBUF is normally a
1219130803Smarcel   copy of the inferior's registers.  */
1220130803Smarcel
122198944Sobrienstruct value *
1222130803Smarcelregister_value_being_returned (struct type *valtype, struct regcache *retbuf)
122319370Spst{
1224130803Smarcel  struct value *val = allocate_value (valtype);
122519370Spst
1226130803Smarcel  /* If the function returns void, don't bother fetching the return
1227130803Smarcel     value.  See also "using_struct_return".  */
1228130803Smarcel  if (TYPE_CODE (valtype) == TYPE_CODE_VOID)
1229130803Smarcel    return val;
123019370Spst
1231130803Smarcel  if (!gdbarch_return_value_p (current_gdbarch))
1232130803Smarcel    {
1233130803Smarcel      /* NOTE: cagney/2003-10-20: Unlike "gdbarch_return_value", the
1234130803Smarcel         EXTRACT_RETURN_VALUE and USE_STRUCT_CONVENTION methods do not
1235130803Smarcel         handle the edge case of a function returning a small
1236130803Smarcel         structure / union in registers.  */
1237130803Smarcel      CHECK_TYPEDEF (valtype);
1238130803Smarcel      EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1239130803Smarcel      return val;
1240130803Smarcel    }
124119370Spst
1242130803Smarcel  /* This function only handles "register convention".  */
1243130803Smarcel  gdb_assert (gdbarch_return_value (current_gdbarch, valtype,
1244130803Smarcel				    NULL, NULL, NULL)
1245130803Smarcel	      == RETURN_VALUE_REGISTER_CONVENTION);
1246130803Smarcel  gdbarch_return_value (current_gdbarch, valtype, retbuf,
1247130803Smarcel			VALUE_CONTENTS_RAW (val) /*read*/, NULL /*write*/);
124819370Spst  return val;
124919370Spst}
125019370Spst
1251130803Smarcel/* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of
1252130803Smarcel   EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc and TYPE
1253130803Smarcel   is the type (which is known to be struct, union or array).
125419370Spst
125519370Spst   On most machines, the struct convention is used unless we are
125619370Spst   using gcc and the type is of a special size.  */
125719370Spst/* As of about 31 Mar 93, GCC was changed to be compatible with the
125819370Spst   native compiler.  GCC 2.3.3 was the last release that did it the
125919370Spst   old way.  Since gcc2_compiled was not changed, we have no
126019370Spst   way to correctly win in all cases, so we just do the right thing
126119370Spst   for gcc1 and for gcc2 after this change.  Thus it loses for gcc
126219370Spst   2.0-2.3.3.  This is somewhat unfortunate, but changing gcc2_compiled
126319370Spst   would cause more chaos than dealing with some struct returns being
126419370Spst   handled wrong.  */
126546283Sdfr
126646283Sdfrint
126798944Sobriengeneric_use_struct_convention (int gcc_p, struct type *value_type)
126898944Sobrien{
126946283Sdfr  return !((gcc_p == 1)
127098944Sobrien	   && (TYPE_LENGTH (value_type) == 1
127198944Sobrien	       || TYPE_LENGTH (value_type) == 2
127298944Sobrien	       || TYPE_LENGTH (value_type) == 4
127398944Sobrien	       || TYPE_LENGTH (value_type) == 8));
127446283Sdfr}
127546283Sdfr
1276130803Smarcel/* Return true if the function returning the specified type is using
1277130803Smarcel   the convention of returning structures in memory (passing in the
1278130803Smarcel   address as a hidden first parameter).  GCC_P is nonzero if compiled
127919370Spst   with GCC.  */
128019370Spst
128119370Spstint
1282130803Smarcelusing_struct_return (struct type *value_type, int gcc_p)
128319370Spst{
1284130803Smarcel  enum type_code code = TYPE_CODE (value_type);
128519370Spst
128619370Spst  if (code == TYPE_CODE_ERROR)
128719370Spst    error ("Function return type unknown.");
128819370Spst
1289130803Smarcel  if (code == TYPE_CODE_VOID)
1290130803Smarcel    /* A void return value is never in memory.  See also corresponding
1291130803Smarcel       code in "register_value_being_returned".  */
1292130803Smarcel    return 0;
129319370Spst
1294130803Smarcel  if (!gdbarch_return_value_p (current_gdbarch))
1295130803Smarcel    {
1296130803Smarcel      /* FIXME: cagney/2003-10-01: The below is dead.  Instead an
1297130803Smarcel	 architecture should implement "gdbarch_return_value".  Using
1298130803Smarcel	 that new function it is possible to exactly specify the ABIs
1299130803Smarcel	 "struct return" vs "register return" conventions.  */
1300130803Smarcel      if (code == TYPE_CODE_STRUCT
1301130803Smarcel	  || code == TYPE_CODE_UNION
1302130803Smarcel	  || code == TYPE_CODE_ARRAY
1303130803Smarcel	  || RETURN_VALUE_ON_STACK (value_type))
1304130803Smarcel	return USE_STRUCT_CONVENTION (gcc_p, value_type);
1305130803Smarcel      else
1306130803Smarcel	return 0;
1307130803Smarcel    }
1308130803Smarcel
1309130803Smarcel  /* Probe the architecture for the return-value convention.  */
1310130803Smarcel  return (gdbarch_return_value (current_gdbarch, value_type,
1311130803Smarcel				NULL, NULL, NULL)
1312130803Smarcel	  == RETURN_VALUE_STRUCT_CONVENTION);
131319370Spst}
131419370Spst
1315283029Ssbruno/* Set the initialized field in a value struct.  */
1316283029Ssbruno
131719370Spstvoid
1318283029Ssbrunoset_value_initialized (struct value *val, int status)
1319283029Ssbruno{
1320283029Ssbruno  val->initialized = status;
1321283029Ssbruno}
1322283029Ssbruno
1323283029Ssbruno/* Return the initialized field in a value struct.  */
1324283029Ssbruno
1325283029Ssbrunoint
1326283029Ssbrunovalue_initialized (struct value *val)
1327283029Ssbruno{
1328283029Ssbruno  return val->initialized;
1329283029Ssbruno}
1330283029Ssbruno
1331283029Ssbrunovoid
133298944Sobrien_initialize_values (void)
133319370Spst{
133419370Spst  add_cmd ("convenience", no_class, show_convenience,
133598944Sobrien	   "Debugger convenience (\"$foo\") variables.\n\
133619370SpstThese variables are created when you assign them values;\n\
133719370Spstthus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
133819370SpstA few convenience variables are given values automatically:\n\
133919370Spst\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
134019370Spst\"$__\" holds the contents of the last address examined with \"x\".",
134119370Spst	   &showlist);
134219370Spst
134319370Spst  add_cmd ("values", no_class, show_values,
134419370Spst	   "Elements of value history around item number IDX (or last ten).",
134519370Spst	   &showlist);
134619370Spst}
1347