xref: /openbsd-src/gnu/usr.bin/binutils/gdb/values.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* Low level packing and unpacking of values for GDB, the GNU Debugger.
2b725ae77Skettenis 
3b725ae77Skettenis    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4b725ae77Skettenis    1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003 Free Software
5b725ae77Skettenis    Foundation, Inc.
6e93f7393Sniklas 
7e93f7393Sniklas    This file is part of GDB.
8e93f7393Sniklas 
9e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas    it under the terms of the GNU General Public License as published by
11e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas    (at your option) any later version.
13e93f7393Sniklas 
14e93f7393Sniklas    This program is distributed in the hope that it will be useful,
15e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e93f7393Sniklas    GNU General Public License for more details.
18e93f7393Sniklas 
19e93f7393Sniklas    You should have received a copy of the GNU General Public License
20e93f7393Sniklas    along with this program; if not, write to the Free Software
21b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
23e93f7393Sniklas 
24e93f7393Sniklas #include "defs.h"
25e93f7393Sniklas #include "gdb_string.h"
26e93f7393Sniklas #include "symtab.h"
27e93f7393Sniklas #include "gdbtypes.h"
28e93f7393Sniklas #include "value.h"
29e93f7393Sniklas #include "gdbcore.h"
30e93f7393Sniklas #include "command.h"
31e93f7393Sniklas #include "gdbcmd.h"
32e93f7393Sniklas #include "target.h"
33e93f7393Sniklas #include "language.h"
34e93f7393Sniklas #include "scm-lang.h"
35e93f7393Sniklas #include "demangle.h"
36b725ae77Skettenis #include "doublest.h"
37b725ae77Skettenis #include "gdb_assert.h"
38b725ae77Skettenis #include "regcache.h"
39b725ae77Skettenis #include "block.h"
40e93f7393Sniklas 
41b725ae77Skettenis /* Prototypes for exported functions. */
42e93f7393Sniklas 
43b725ae77Skettenis void _initialize_values (void);
44e93f7393Sniklas 
45b725ae77Skettenis /* Prototypes for local functions. */
46e93f7393Sniklas 
47b725ae77Skettenis static void show_values (char *, int);
48e93f7393Sniklas 
49b725ae77Skettenis static void show_convenience (char *, int);
50b725ae77Skettenis 
51e93f7393Sniklas 
52e93f7393Sniklas /* The value-history records all the values printed
53e93f7393Sniklas    by print commands during this session.  Each chunk
54e93f7393Sniklas    records 60 consecutive values.  The first chunk on
55e93f7393Sniklas    the chain records the most recent values.
56e93f7393Sniklas    The total number of values is in value_history_count.  */
57e93f7393Sniklas 
58e93f7393Sniklas #define VALUE_HISTORY_CHUNK 60
59e93f7393Sniklas 
60e93f7393Sniklas struct value_history_chunk
61e93f7393Sniklas   {
62e93f7393Sniklas     struct value_history_chunk *next;
63b725ae77Skettenis     struct value *values[VALUE_HISTORY_CHUNK];
64e93f7393Sniklas   };
65e93f7393Sniklas 
66e93f7393Sniklas /* Chain of chunks now in use.  */
67e93f7393Sniklas 
68e93f7393Sniklas static struct value_history_chunk *value_history_chain;
69e93f7393Sniklas 
70e93f7393Sniklas static int value_history_count;	/* Abs number of last entry stored */
71e93f7393Sniklas 
72e93f7393Sniklas /* List of all value objects currently allocated
73e93f7393Sniklas    (except for those released by calls to release_value)
74e93f7393Sniklas    This is so they can be freed after each command.  */
75e93f7393Sniklas 
76b725ae77Skettenis static struct value *all_values;
77e93f7393Sniklas 
78e93f7393Sniklas /* Allocate a  value  that has the correct length for type TYPE.  */
79e93f7393Sniklas 
80b725ae77Skettenis struct value *
allocate_value(struct type * type)81b725ae77Skettenis allocate_value (struct type *type)
82e93f7393Sniklas {
83b725ae77Skettenis   struct value *val;
84e93f7393Sniklas   struct type *atype = check_typedef (type);
85e93f7393Sniklas 
86e93f7393Sniklas   val = (struct value *) xmalloc (sizeof (struct value) + TYPE_LENGTH (atype));
87e93f7393Sniklas   VALUE_NEXT (val) = all_values;
88e93f7393Sniklas   all_values = val;
89e93f7393Sniklas   VALUE_TYPE (val) = type;
90b725ae77Skettenis   VALUE_ENCLOSING_TYPE (val) = type;
91e93f7393Sniklas   VALUE_LVAL (val) = not_lval;
92e93f7393Sniklas   VALUE_ADDRESS (val) = 0;
93b725ae77Skettenis   VALUE_FRAME_ID (val) = null_frame_id;
94e93f7393Sniklas   VALUE_OFFSET (val) = 0;
95e93f7393Sniklas   VALUE_BITPOS (val) = 0;
96e93f7393Sniklas   VALUE_BITSIZE (val) = 0;
97e93f7393Sniklas   VALUE_REGNO (val) = -1;
98e93f7393Sniklas   VALUE_LAZY (val) = 0;
99e93f7393Sniklas   VALUE_OPTIMIZED_OUT (val) = 0;
100b725ae77Skettenis   VALUE_BFD_SECTION (val) = NULL;
101b725ae77Skettenis   VALUE_EMBEDDED_OFFSET (val) = 0;
102b725ae77Skettenis   VALUE_POINTED_TO_OFFSET (val) = 0;
103e93f7393Sniklas   val->modifiable = 1;
104e93f7393Sniklas   return val;
105e93f7393Sniklas }
106e93f7393Sniklas 
107e93f7393Sniklas /* Allocate a  value  that has the correct length
108e93f7393Sniklas    for COUNT repetitions type TYPE.  */
109e93f7393Sniklas 
110b725ae77Skettenis struct value *
allocate_repeat_value(struct type * type,int count)111b725ae77Skettenis allocate_repeat_value (struct type *type, int count)
112e93f7393Sniklas {
113e93f7393Sniklas   int low_bound = current_language->string_lower_bound;		/* ??? */
114e93f7393Sniklas   /* FIXME-type-allocation: need a way to free this type when we are
115e93f7393Sniklas      done with it.  */
116e93f7393Sniklas   struct type *range_type
117e93f7393Sniklas   = create_range_type ((struct type *) NULL, builtin_type_int,
118e93f7393Sniklas 		       low_bound, count + low_bound - 1);
119e93f7393Sniklas   /* FIXME-type-allocation: need a way to free this type when we are
120e93f7393Sniklas      done with it.  */
121e93f7393Sniklas   return allocate_value (create_array_type ((struct type *) NULL,
122e93f7393Sniklas 					    type, range_type));
123e93f7393Sniklas }
124e93f7393Sniklas 
125e93f7393Sniklas /* Return a mark in the value chain.  All values allocated after the
126e93f7393Sniklas    mark is obtained (except for those released) are subject to being freed
127e93f7393Sniklas    if a subsequent value_free_to_mark is passed the mark.  */
128b725ae77Skettenis struct value *
value_mark(void)129b725ae77Skettenis value_mark (void)
130e93f7393Sniklas {
131e93f7393Sniklas   return all_values;
132e93f7393Sniklas }
133e93f7393Sniklas 
134e93f7393Sniklas /* Free all values allocated since MARK was obtained by value_mark
135e93f7393Sniklas    (except for those released).  */
136e93f7393Sniklas void
value_free_to_mark(struct value * mark)137b725ae77Skettenis value_free_to_mark (struct value *mark)
138e93f7393Sniklas {
139b725ae77Skettenis   struct value *val;
140b725ae77Skettenis   struct value *next;
141e93f7393Sniklas 
142e93f7393Sniklas   for (val = all_values; val && val != mark; val = next)
143e93f7393Sniklas     {
144e93f7393Sniklas       next = VALUE_NEXT (val);
145e93f7393Sniklas       value_free (val);
146e93f7393Sniklas     }
147e93f7393Sniklas   all_values = val;
148e93f7393Sniklas }
149e93f7393Sniklas 
150e93f7393Sniklas /* Free all the values that have been allocated (except for those released).
151e93f7393Sniklas    Called after each command, successful or not.  */
152e93f7393Sniklas 
153e93f7393Sniklas void
free_all_values(void)154b725ae77Skettenis free_all_values (void)
155e93f7393Sniklas {
156b725ae77Skettenis   struct value *val;
157b725ae77Skettenis   struct value *next;
158e93f7393Sniklas 
159e93f7393Sniklas   for (val = all_values; val; val = next)
160e93f7393Sniklas     {
161e93f7393Sniklas       next = VALUE_NEXT (val);
162e93f7393Sniklas       value_free (val);
163e93f7393Sniklas     }
164e93f7393Sniklas 
165e93f7393Sniklas   all_values = 0;
166e93f7393Sniklas }
167e93f7393Sniklas 
168e93f7393Sniklas /* Remove VAL from the chain all_values
169e93f7393Sniklas    so it will not be freed automatically.  */
170e93f7393Sniklas 
171e93f7393Sniklas void
release_value(struct value * val)172b725ae77Skettenis release_value (struct value *val)
173e93f7393Sniklas {
174b725ae77Skettenis   struct value *v;
175e93f7393Sniklas 
176e93f7393Sniklas   if (all_values == val)
177e93f7393Sniklas     {
178e93f7393Sniklas       all_values = val->next;
179e93f7393Sniklas       return;
180e93f7393Sniklas     }
181e93f7393Sniklas 
182e93f7393Sniklas   for (v = all_values; v; v = v->next)
183e93f7393Sniklas     {
184e93f7393Sniklas       if (v->next == val)
185e93f7393Sniklas 	{
186e93f7393Sniklas 	  v->next = val->next;
187e93f7393Sniklas 	  break;
188e93f7393Sniklas 	}
189e93f7393Sniklas     }
190e93f7393Sniklas }
191e93f7393Sniklas 
192e93f7393Sniklas /* Release all values up to mark  */
193b725ae77Skettenis struct value *
value_release_to_mark(struct value * mark)194b725ae77Skettenis value_release_to_mark (struct value *mark)
195e93f7393Sniklas {
196b725ae77Skettenis   struct value *val;
197b725ae77Skettenis   struct value *next;
198e93f7393Sniklas 
199e93f7393Sniklas   for (val = next = all_values; next; next = VALUE_NEXT (next))
200e93f7393Sniklas     if (VALUE_NEXT (next) == mark)
201e93f7393Sniklas       {
202e93f7393Sniklas 	all_values = VALUE_NEXT (next);
203e93f7393Sniklas 	VALUE_NEXT (next) = 0;
204e93f7393Sniklas 	return val;
205e93f7393Sniklas       }
206e93f7393Sniklas   all_values = 0;
207e93f7393Sniklas   return val;
208e93f7393Sniklas }
209e93f7393Sniklas 
210e93f7393Sniklas /* Return a copy of the value ARG.
211e93f7393Sniklas    It contains the same contents, for same memory address,
212e93f7393Sniklas    but it's a different block of storage.  */
213e93f7393Sniklas 
214b725ae77Skettenis struct value *
value_copy(struct value * arg)215b725ae77Skettenis value_copy (struct value *arg)
216e93f7393Sniklas {
217b725ae77Skettenis   struct type *encl_type = VALUE_ENCLOSING_TYPE (arg);
218b725ae77Skettenis   struct value *val = allocate_value (encl_type);
219b725ae77Skettenis   VALUE_TYPE (val) = VALUE_TYPE (arg);
220e93f7393Sniklas   VALUE_LVAL (val) = VALUE_LVAL (arg);
221e93f7393Sniklas   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
222e93f7393Sniklas   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
223e93f7393Sniklas   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
224e93f7393Sniklas   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
225b725ae77Skettenis   VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
226e93f7393Sniklas   VALUE_REGNO (val) = VALUE_REGNO (arg);
227e93f7393Sniklas   VALUE_LAZY (val) = VALUE_LAZY (arg);
228e93f7393Sniklas   VALUE_OPTIMIZED_OUT (val) = VALUE_OPTIMIZED_OUT (arg);
229b725ae77Skettenis   VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (arg);
230b725ae77Skettenis   VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (arg);
231b725ae77Skettenis   VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (arg);
232e93f7393Sniklas   val->modifiable = arg->modifiable;
233e93f7393Sniklas   if (!VALUE_LAZY (val))
234e93f7393Sniklas     {
235b725ae77Skettenis       memcpy (VALUE_CONTENTS_ALL_RAW (val), VALUE_CONTENTS_ALL_RAW (arg),
236b725ae77Skettenis 	      TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg)));
237b725ae77Skettenis 
238e93f7393Sniklas     }
239e93f7393Sniklas   return val;
240e93f7393Sniklas }
241e93f7393Sniklas 
242e93f7393Sniklas /* Access to the value history.  */
243e93f7393Sniklas 
244e93f7393Sniklas /* Record a new value in the value history.
245e93f7393Sniklas    Returns the absolute history index of the entry.
246e93f7393Sniklas    Result of -1 indicates the value was not saved; otherwise it is the
247e93f7393Sniklas    value history index of this new item.  */
248e93f7393Sniklas 
249e93f7393Sniklas int
record_latest_value(struct value * val)250b725ae77Skettenis record_latest_value (struct value *val)
251e93f7393Sniklas {
252e93f7393Sniklas   int i;
253e93f7393Sniklas 
254e93f7393Sniklas   /* We don't want this value to have anything to do with the inferior anymore.
255e93f7393Sniklas      In particular, "set $1 = 50" should not affect the variable from which
256e93f7393Sniklas      the value was taken, and fast watchpoints should be able to assume that
257e93f7393Sniklas      a value on the value history never changes.  */
258e93f7393Sniklas   if (VALUE_LAZY (val))
259e93f7393Sniklas     value_fetch_lazy (val);
260e93f7393Sniklas   /* We preserve VALUE_LVAL so that the user can find out where it was fetched
261e93f7393Sniklas      from.  This is a bit dubious, because then *&$1 does not just return $1
262e93f7393Sniklas      but the current contents of that location.  c'est la vie...  */
263e93f7393Sniklas   val->modifiable = 0;
264e93f7393Sniklas   release_value (val);
265e93f7393Sniklas 
266e93f7393Sniklas   /* Here we treat value_history_count as origin-zero
267e93f7393Sniklas      and applying to the value being stored now.  */
268e93f7393Sniklas 
269e93f7393Sniklas   i = value_history_count % VALUE_HISTORY_CHUNK;
270e93f7393Sniklas   if (i == 0)
271e93f7393Sniklas     {
272b725ae77Skettenis       struct value_history_chunk *new
273e93f7393Sniklas       = (struct value_history_chunk *)
274e93f7393Sniklas       xmalloc (sizeof (struct value_history_chunk));
275e93f7393Sniklas       memset (new->values, 0, sizeof new->values);
276e93f7393Sniklas       new->next = value_history_chain;
277e93f7393Sniklas       value_history_chain = new;
278e93f7393Sniklas     }
279e93f7393Sniklas 
280e93f7393Sniklas   value_history_chain->values[i] = val;
281e93f7393Sniklas 
282e93f7393Sniklas   /* Now we regard value_history_count as origin-one
283e93f7393Sniklas      and applying to the value just stored.  */
284e93f7393Sniklas 
285e93f7393Sniklas   return ++value_history_count;
286e93f7393Sniklas }
287e93f7393Sniklas 
288e93f7393Sniklas /* Return a copy of the value in the history with sequence number NUM.  */
289e93f7393Sniklas 
290b725ae77Skettenis struct value *
access_value_history(int num)291b725ae77Skettenis access_value_history (int num)
292e93f7393Sniklas {
293b725ae77Skettenis   struct value_history_chunk *chunk;
294b725ae77Skettenis   int i;
295b725ae77Skettenis   int absnum = num;
296e93f7393Sniklas 
297e93f7393Sniklas   if (absnum <= 0)
298e93f7393Sniklas     absnum += value_history_count;
299e93f7393Sniklas 
300e93f7393Sniklas   if (absnum <= 0)
301e93f7393Sniklas     {
302e93f7393Sniklas       if (num == 0)
303e93f7393Sniklas 	error ("The history is empty.");
304e93f7393Sniklas       else if (num == 1)
305e93f7393Sniklas 	error ("There is only one value in the history.");
306e93f7393Sniklas       else
307e93f7393Sniklas 	error ("History does not go back to $$%d.", -num);
308e93f7393Sniklas     }
309e93f7393Sniklas   if (absnum > value_history_count)
310e93f7393Sniklas     error ("History has not yet reached $%d.", absnum);
311e93f7393Sniklas 
312e93f7393Sniklas   absnum--;
313e93f7393Sniklas 
314e93f7393Sniklas   /* Now absnum is always absolute and origin zero.  */
315e93f7393Sniklas 
316e93f7393Sniklas   chunk = value_history_chain;
317e93f7393Sniklas   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
318e93f7393Sniklas        i > 0; i--)
319e93f7393Sniklas     chunk = chunk->next;
320e93f7393Sniklas 
321e93f7393Sniklas   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
322e93f7393Sniklas }
323e93f7393Sniklas 
324e93f7393Sniklas /* Clear the value history entirely.
325e93f7393Sniklas    Must be done when new symbol tables are loaded,
326e93f7393Sniklas    because the type pointers become invalid.  */
327e93f7393Sniklas 
328e93f7393Sniklas void
clear_value_history(void)329b725ae77Skettenis clear_value_history (void)
330e93f7393Sniklas {
331b725ae77Skettenis   struct value_history_chunk *next;
332b725ae77Skettenis   int i;
333b725ae77Skettenis   struct value *val;
334e93f7393Sniklas 
335e93f7393Sniklas   while (value_history_chain)
336e93f7393Sniklas     {
337e93f7393Sniklas       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
338e93f7393Sniklas 	if ((val = value_history_chain->values[i]) != NULL)
339b725ae77Skettenis 	  xfree (val);
340e93f7393Sniklas       next = value_history_chain->next;
341b725ae77Skettenis       xfree (value_history_chain);
342e93f7393Sniklas       value_history_chain = next;
343e93f7393Sniklas     }
344e93f7393Sniklas   value_history_count = 0;
345e93f7393Sniklas }
346e93f7393Sniklas 
347e93f7393Sniklas static void
show_values(char * num_exp,int from_tty)348b725ae77Skettenis show_values (char *num_exp, int from_tty)
349e93f7393Sniklas {
350b725ae77Skettenis   int i;
351b725ae77Skettenis   struct value *val;
352e93f7393Sniklas   static int num = 1;
353e93f7393Sniklas 
354e93f7393Sniklas   if (num_exp)
355e93f7393Sniklas     {
356e93f7393Sniklas       /* "info history +" should print from the stored position.
357e93f7393Sniklas          "info history <exp>" should print around value number <exp>.  */
358e93f7393Sniklas       if (num_exp[0] != '+' || num_exp[1] != '\0')
359b725ae77Skettenis 	num = parse_and_eval_long (num_exp) - 5;
360e93f7393Sniklas     }
361e93f7393Sniklas   else
362e93f7393Sniklas     {
363e93f7393Sniklas       /* "info history" means print the last 10 values.  */
364e93f7393Sniklas       num = value_history_count - 9;
365e93f7393Sniklas     }
366e93f7393Sniklas 
367e93f7393Sniklas   if (num <= 0)
368e93f7393Sniklas     num = 1;
369e93f7393Sniklas 
370e93f7393Sniklas   for (i = num; i < num + 10 && i <= value_history_count; i++)
371e93f7393Sniklas     {
372e93f7393Sniklas       val = access_value_history (i);
373e93f7393Sniklas       printf_filtered ("$%d = ", i);
374e93f7393Sniklas       value_print (val, gdb_stdout, 0, Val_pretty_default);
375e93f7393Sniklas       printf_filtered ("\n");
376e93f7393Sniklas     }
377e93f7393Sniklas 
378e93f7393Sniklas   /* The next "info history +" should start after what we just printed.  */
379e93f7393Sniklas   num += 10;
380e93f7393Sniklas 
381e93f7393Sniklas   /* Hitting just return after this command should do the same thing as
382e93f7393Sniklas      "info history +".  If num_exp is null, this is unnecessary, since
383e93f7393Sniklas      "info history +" is not useful after "info history".  */
384e93f7393Sniklas   if (from_tty && num_exp)
385e93f7393Sniklas     {
386e93f7393Sniklas       num_exp[0] = '+';
387e93f7393Sniklas       num_exp[1] = '\0';
388e93f7393Sniklas     }
389e93f7393Sniklas }
390e93f7393Sniklas 
391e93f7393Sniklas /* Internal variables.  These are variables within the debugger
392e93f7393Sniklas    that hold values assigned by debugger commands.
393e93f7393Sniklas    The user refers to them with a '$' prefix
394e93f7393Sniklas    that does not appear in the variable names stored internally.  */
395e93f7393Sniklas 
396e93f7393Sniklas static struct internalvar *internalvars;
397e93f7393Sniklas 
398e93f7393Sniklas /* Look up an internal variable with name NAME.  NAME should not
399e93f7393Sniklas    normally include a dollar sign.
400e93f7393Sniklas 
401e93f7393Sniklas    If the specified internal variable does not exist,
402e93f7393Sniklas    one is created, with a void value.  */
403e93f7393Sniklas 
404e93f7393Sniklas struct internalvar *
lookup_internalvar(char * name)405b725ae77Skettenis lookup_internalvar (char *name)
406e93f7393Sniklas {
407b725ae77Skettenis   struct internalvar *var;
408e93f7393Sniklas 
409e93f7393Sniklas   for (var = internalvars; var; var = var->next)
410b725ae77Skettenis     if (strcmp (var->name, name) == 0)
411e93f7393Sniklas       return var;
412e93f7393Sniklas 
413e93f7393Sniklas   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
414e93f7393Sniklas   var->name = concat (name, NULL);
415e93f7393Sniklas   var->value = allocate_value (builtin_type_void);
416e93f7393Sniklas   release_value (var->value);
417e93f7393Sniklas   var->next = internalvars;
418e93f7393Sniklas   internalvars = var;
419e93f7393Sniklas   return var;
420e93f7393Sniklas }
421e93f7393Sniklas 
422b725ae77Skettenis struct value *
value_of_internalvar(struct internalvar * var)423b725ae77Skettenis value_of_internalvar (struct internalvar *var)
424e93f7393Sniklas {
425b725ae77Skettenis   struct value *val;
426e93f7393Sniklas 
427e93f7393Sniklas   val = value_copy (var->value);
428e93f7393Sniklas   if (VALUE_LAZY (val))
429e93f7393Sniklas     value_fetch_lazy (val);
430e93f7393Sniklas   VALUE_LVAL (val) = lval_internalvar;
431e93f7393Sniklas   VALUE_INTERNALVAR (val) = var;
432e93f7393Sniklas   return val;
433e93f7393Sniklas }
434e93f7393Sniklas 
435e93f7393Sniklas void
set_internalvar_component(struct internalvar * var,int offset,int bitpos,int bitsize,struct value * newval)436b725ae77Skettenis set_internalvar_component (struct internalvar *var, int offset, int bitpos,
437b725ae77Skettenis 			   int bitsize, struct value *newval)
438e93f7393Sniklas {
439b725ae77Skettenis   char *addr = VALUE_CONTENTS (var->value) + offset;
440e93f7393Sniklas 
441e93f7393Sniklas   if (bitsize)
442e93f7393Sniklas     modify_field (addr, value_as_long (newval),
443e93f7393Sniklas 		  bitpos, bitsize);
444e93f7393Sniklas   else
445e93f7393Sniklas     memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval)));
446e93f7393Sniklas }
447e93f7393Sniklas 
448e93f7393Sniklas void
set_internalvar(struct internalvar * var,struct value * val)449b725ae77Skettenis set_internalvar (struct internalvar *var, struct value *val)
450e93f7393Sniklas {
451b725ae77Skettenis   struct value *newval;
452e93f7393Sniklas 
453e93f7393Sniklas   newval = value_copy (val);
454e93f7393Sniklas   newval->modifiable = 1;
455e93f7393Sniklas 
456e93f7393Sniklas   /* Force the value to be fetched from the target now, to avoid problems
457e93f7393Sniklas      later when this internalvar is referenced and the target is gone or
458e93f7393Sniklas      has changed.  */
459e93f7393Sniklas   if (VALUE_LAZY (newval))
460e93f7393Sniklas     value_fetch_lazy (newval);
461e93f7393Sniklas 
462e93f7393Sniklas   /* Begin code which must not call error().  If var->value points to
463e93f7393Sniklas      something free'd, an error() obviously leaves a dangling pointer.
464e93f7393Sniklas      But we also get a danling pointer if var->value points to
465e93f7393Sniklas      something in the value chain (i.e., before release_value is
466e93f7393Sniklas      called), because after the error free_all_values will get called before
467e93f7393Sniklas      long.  */
468b725ae77Skettenis   xfree (var->value);
469e93f7393Sniklas   var->value = newval;
470e93f7393Sniklas   release_value (newval);
471e93f7393Sniklas   /* End code which must not call error().  */
472e93f7393Sniklas }
473e93f7393Sniklas 
474e93f7393Sniklas char *
internalvar_name(struct internalvar * var)475b725ae77Skettenis internalvar_name (struct internalvar *var)
476e93f7393Sniklas {
477e93f7393Sniklas   return var->name;
478e93f7393Sniklas }
479e93f7393Sniklas 
480e93f7393Sniklas /* Free all internalvars.  Done when new symtabs are loaded,
481e93f7393Sniklas    because that makes the values invalid.  */
482e93f7393Sniklas 
483e93f7393Sniklas void
clear_internalvars(void)484b725ae77Skettenis clear_internalvars (void)
485e93f7393Sniklas {
486b725ae77Skettenis   struct internalvar *var;
487e93f7393Sniklas 
488e93f7393Sniklas   while (internalvars)
489e93f7393Sniklas     {
490e93f7393Sniklas       var = internalvars;
491e93f7393Sniklas       internalvars = var->next;
492b725ae77Skettenis       xfree (var->name);
493b725ae77Skettenis       xfree (var->value);
494b725ae77Skettenis       xfree (var);
495e93f7393Sniklas     }
496e93f7393Sniklas }
497e93f7393Sniklas 
498e93f7393Sniklas static void
show_convenience(char * ignore,int from_tty)499b725ae77Skettenis show_convenience (char *ignore, int from_tty)
500e93f7393Sniklas {
501b725ae77Skettenis   struct internalvar *var;
502e93f7393Sniklas   int varseen = 0;
503e93f7393Sniklas 
504e93f7393Sniklas   for (var = internalvars; var; var = var->next)
505e93f7393Sniklas     {
506e93f7393Sniklas       if (!varseen)
507e93f7393Sniklas 	{
508e93f7393Sniklas 	  varseen = 1;
509e93f7393Sniklas 	}
510e93f7393Sniklas       printf_filtered ("$%s = ", var->name);
511e93f7393Sniklas       value_print (var->value, gdb_stdout, 0, Val_pretty_default);
512e93f7393Sniklas       printf_filtered ("\n");
513e93f7393Sniklas     }
514e93f7393Sniklas   if (!varseen)
515e93f7393Sniklas     printf_unfiltered ("No debugger convenience variables now defined.\n\
516e93f7393Sniklas Convenience variables have names starting with \"$\";\n\
517e93f7393Sniklas use \"set\" as in \"set $foo = 5\" to define them.\n");
518e93f7393Sniklas }
519e93f7393Sniklas 
520e93f7393Sniklas /* Extract a value as a C number (either long or double).
521e93f7393Sniklas    Knows how to convert fixed values to double, or
522e93f7393Sniklas    floating values to long.
523e93f7393Sniklas    Does not deallocate the value.  */
524e93f7393Sniklas 
525e93f7393Sniklas LONGEST
value_as_long(struct value * val)526b725ae77Skettenis value_as_long (struct value *val)
527e93f7393Sniklas {
528e93f7393Sniklas   /* This coerces arrays and functions, which is necessary (e.g.
529e93f7393Sniklas      in disassemble_command).  It also dereferences references, which
530e93f7393Sniklas      I suspect is the most logical thing to do.  */
531e93f7393Sniklas   COERCE_ARRAY (val);
532e93f7393Sniklas   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
533e93f7393Sniklas }
534e93f7393Sniklas 
535e93f7393Sniklas DOUBLEST
value_as_double(struct value * val)536b725ae77Skettenis value_as_double (struct value *val)
537e93f7393Sniklas {
538e93f7393Sniklas   DOUBLEST foo;
539e93f7393Sniklas   int inv;
540e93f7393Sniklas 
541e93f7393Sniklas   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
542e93f7393Sniklas   if (inv)
543e93f7393Sniklas     error ("Invalid floating value found in program.");
544e93f7393Sniklas   return foo;
545e93f7393Sniklas }
546b725ae77Skettenis /* Extract a value as a C pointer. Does not deallocate the value.
547b725ae77Skettenis    Note that val's type may not actually be a pointer; value_as_long
548b725ae77Skettenis    handles all the cases.  */
549e93f7393Sniklas CORE_ADDR
value_as_address(struct value * val)550b725ae77Skettenis value_as_address (struct value *val)
551e93f7393Sniklas {
552e93f7393Sniklas   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
553e93f7393Sniklas      whether we want this to be true eventually.  */
554e93f7393Sniklas #if 0
555e93f7393Sniklas   /* ADDR_BITS_REMOVE is wrong if we are being called for a
556e93f7393Sniklas      non-address (e.g. argument to "signal", "info break", etc.), or
557e93f7393Sniklas      for pointers to char, in which the low bits *are* significant.  */
558e93f7393Sniklas   return ADDR_BITS_REMOVE (value_as_long (val));
559e93f7393Sniklas #else
560b725ae77Skettenis 
561b725ae77Skettenis   /* There are several targets (IA-64, PowerPC, and others) which
562b725ae77Skettenis      don't represent pointers to functions as simply the address of
563b725ae77Skettenis      the function's entry point.  For example, on the IA-64, a
564b725ae77Skettenis      function pointer points to a two-word descriptor, generated by
565b725ae77Skettenis      the linker, which contains the function's entry point, and the
566b725ae77Skettenis      value the IA-64 "global pointer" register should have --- to
567b725ae77Skettenis      support position-independent code.  The linker generates
568b725ae77Skettenis      descriptors only for those functions whose addresses are taken.
569b725ae77Skettenis 
570b725ae77Skettenis      On such targets, it's difficult for GDB to convert an arbitrary
571b725ae77Skettenis      function address into a function pointer; it has to either find
572b725ae77Skettenis      an existing descriptor for that function, or call malloc and
573b725ae77Skettenis      build its own.  On some targets, it is impossible for GDB to
574b725ae77Skettenis      build a descriptor at all: the descriptor must contain a jump
575b725ae77Skettenis      instruction; data memory cannot be executed; and code memory
576b725ae77Skettenis      cannot be modified.
577b725ae77Skettenis 
578b725ae77Skettenis      Upon entry to this function, if VAL is a value of type `function'
579b725ae77Skettenis      (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
580b725ae77Skettenis      VALUE_ADDRESS (val) is the address of the function.  This is what
581b725ae77Skettenis      you'll get if you evaluate an expression like `main'.  The call
582b725ae77Skettenis      to COERCE_ARRAY below actually does all the usual unary
583b725ae77Skettenis      conversions, which includes converting values of type `function'
584b725ae77Skettenis      to `pointer to function'.  This is the challenging conversion
585b725ae77Skettenis      discussed above.  Then, `unpack_long' will convert that pointer
586b725ae77Skettenis      back into an address.
587b725ae77Skettenis 
588b725ae77Skettenis      So, suppose the user types `disassemble foo' on an architecture
589b725ae77Skettenis      with a strange function pointer representation, on which GDB
590b725ae77Skettenis      cannot build its own descriptors, and suppose further that `foo'
591b725ae77Skettenis      has no linker-built descriptor.  The address->pointer conversion
592b725ae77Skettenis      will signal an error and prevent the command from running, even
593b725ae77Skettenis      though the next step would have been to convert the pointer
594b725ae77Skettenis      directly back into the same address.
595b725ae77Skettenis 
596b725ae77Skettenis      The following shortcut avoids this whole mess.  If VAL is a
597b725ae77Skettenis      function, just return its address directly.  */
598b725ae77Skettenis   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
599b725ae77Skettenis       || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_METHOD)
600b725ae77Skettenis     return VALUE_ADDRESS (val);
601b725ae77Skettenis 
602b725ae77Skettenis   COERCE_ARRAY (val);
603b725ae77Skettenis 
604b725ae77Skettenis   /* Some architectures (e.g. Harvard), map instruction and data
605b725ae77Skettenis      addresses onto a single large unified address space.  For
606b725ae77Skettenis      instance: An architecture may consider a large integer in the
607b725ae77Skettenis      range 0x10000000 .. 0x1000ffff to already represent a data
608b725ae77Skettenis      addresses (hence not need a pointer to address conversion) while
609b725ae77Skettenis      a small integer would still need to be converted integer to
610b725ae77Skettenis      pointer to address.  Just assume such architectures handle all
611b725ae77Skettenis      integer conversions in a single function.  */
612b725ae77Skettenis 
613b725ae77Skettenis   /* JimB writes:
614b725ae77Skettenis 
615b725ae77Skettenis      I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
616b725ae77Skettenis      must admonish GDB hackers to make sure its behavior matches the
617b725ae77Skettenis      compiler's, whenever possible.
618b725ae77Skettenis 
619b725ae77Skettenis      In general, I think GDB should evaluate expressions the same way
620b725ae77Skettenis      the compiler does.  When the user copies an expression out of
621b725ae77Skettenis      their source code and hands it to a `print' command, they should
622b725ae77Skettenis      get the same value the compiler would have computed.  Any
623b725ae77Skettenis      deviation from this rule can cause major confusion and annoyance,
624b725ae77Skettenis      and needs to be justified carefully.  In other words, GDB doesn't
625b725ae77Skettenis      really have the freedom to do these conversions in clever and
626b725ae77Skettenis      useful ways.
627b725ae77Skettenis 
628b725ae77Skettenis      AndrewC pointed out that users aren't complaining about how GDB
629b725ae77Skettenis      casts integers to pointers; they are complaining that they can't
630b725ae77Skettenis      take an address from a disassembly listing and give it to `x/i'.
631b725ae77Skettenis      This is certainly important.
632b725ae77Skettenis 
633b725ae77Skettenis      Adding an architecture method like INTEGER_TO_ADDRESS certainly
634b725ae77Skettenis      makes it possible for GDB to "get it right" in all circumstances
635b725ae77Skettenis      --- the target has complete control over how things get done, so
636b725ae77Skettenis      people can Do The Right Thing for their target without breaking
637b725ae77Skettenis      anyone else.  The standard doesn't specify how integers get
638b725ae77Skettenis      converted to pointers; usually, the ABI doesn't either, but
639b725ae77Skettenis      ABI-specific code is a more reasonable place to handle it.  */
640b725ae77Skettenis 
641b725ae77Skettenis   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_PTR
642b725ae77Skettenis       && TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_REF
643b725ae77Skettenis       && INTEGER_TO_ADDRESS_P ())
644b725ae77Skettenis     return INTEGER_TO_ADDRESS (VALUE_TYPE (val), VALUE_CONTENTS (val));
645b725ae77Skettenis 
646b725ae77Skettenis   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
647e93f7393Sniklas #endif
648e93f7393Sniklas }
649e93f7393Sniklas 
650e93f7393Sniklas /* Unpack raw data (copied from debugee, target byte order) at VALADDR
651e93f7393Sniklas    as a long, or as a double, assuming the raw data is described
652e93f7393Sniklas    by type TYPE.  Knows how to convert different sizes of values
653e93f7393Sniklas    and can convert between fixed and floating point.  We don't assume
654e93f7393Sniklas    any alignment for the raw data.  Return value is in host byte order.
655e93f7393Sniklas 
656e93f7393Sniklas    If you want functions and arrays to be coerced to pointers, and
657e93f7393Sniklas    references to be dereferenced, call value_as_long() instead.
658e93f7393Sniklas 
659e93f7393Sniklas    C++: It is assumed that the front-end has taken care of
660e93f7393Sniklas    all matters concerning pointers to members.  A pointer
661e93f7393Sniklas    to member which reaches here is considered to be equivalent
662e93f7393Sniklas    to an INT (or some size).  After all, it is only an offset.  */
663e93f7393Sniklas 
664e93f7393Sniklas LONGEST
unpack_long(struct type * type,const char * valaddr)665b725ae77Skettenis unpack_long (struct type *type, const char *valaddr)
666e93f7393Sniklas {
667b725ae77Skettenis   enum type_code code = TYPE_CODE (type);
668b725ae77Skettenis   int len = TYPE_LENGTH (type);
669b725ae77Skettenis   int nosign = TYPE_UNSIGNED (type);
670e93f7393Sniklas 
671e93f7393Sniklas   if (current_language->la_language == language_scm
672e93f7393Sniklas       && is_scmvalue_type (type))
673e93f7393Sniklas     return scm_unpack (type, valaddr, TYPE_CODE_INT);
674e93f7393Sniklas 
675e93f7393Sniklas   switch (code)
676e93f7393Sniklas     {
677e93f7393Sniklas     case TYPE_CODE_TYPEDEF:
678e93f7393Sniklas       return unpack_long (check_typedef (type), valaddr);
679e93f7393Sniklas     case TYPE_CODE_ENUM:
680e93f7393Sniklas     case TYPE_CODE_BOOL:
681e93f7393Sniklas     case TYPE_CODE_INT:
682e93f7393Sniklas     case TYPE_CODE_CHAR:
683e93f7393Sniklas     case TYPE_CODE_RANGE:
684e93f7393Sniklas       if (nosign)
685e93f7393Sniklas 	return extract_unsigned_integer (valaddr, len);
686e93f7393Sniklas       else
687e93f7393Sniklas 	return extract_signed_integer (valaddr, len);
688e93f7393Sniklas 
689e93f7393Sniklas     case TYPE_CODE_FLT:
690b725ae77Skettenis       return extract_typed_floating (valaddr, type);
691e93f7393Sniklas 
692e93f7393Sniklas     case TYPE_CODE_PTR:
693e93f7393Sniklas     case TYPE_CODE_REF:
694e93f7393Sniklas       /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
695e93f7393Sniklas          whether we want this to be true eventually.  */
696b725ae77Skettenis       return extract_typed_address (valaddr, type);
697e93f7393Sniklas 
698e93f7393Sniklas     case TYPE_CODE_MEMBER:
699e93f7393Sniklas       error ("not implemented: member types in unpack_long");
700e93f7393Sniklas 
701e93f7393Sniklas     default:
702e93f7393Sniklas       error ("Value can't be converted to integer.");
703e93f7393Sniklas     }
704e93f7393Sniklas   return 0;			/* Placate lint.  */
705e93f7393Sniklas }
706e93f7393Sniklas 
707e93f7393Sniklas /* Return a double value from the specified type and address.
708e93f7393Sniklas    INVP points to an int which is set to 0 for valid value,
709e93f7393Sniklas    1 for invalid value (bad float format).  In either case,
710e93f7393Sniklas    the returned double is OK to use.  Argument is in target
711e93f7393Sniklas    format, result is in host format.  */
712e93f7393Sniklas 
713e93f7393Sniklas DOUBLEST
unpack_double(struct type * type,const char * valaddr,int * invp)714b725ae77Skettenis unpack_double (struct type *type, const char *valaddr, int *invp)
715e93f7393Sniklas {
716b725ae77Skettenis   enum type_code code;
717b725ae77Skettenis   int len;
718b725ae77Skettenis   int nosign;
719e93f7393Sniklas 
720e93f7393Sniklas   *invp = 0;			/* Assume valid.   */
721e93f7393Sniklas   CHECK_TYPEDEF (type);
722b725ae77Skettenis   code = TYPE_CODE (type);
723b725ae77Skettenis   len = TYPE_LENGTH (type);
724b725ae77Skettenis   nosign = TYPE_UNSIGNED (type);
725e93f7393Sniklas   if (code == TYPE_CODE_FLT)
726e93f7393Sniklas     {
727b725ae77Skettenis       /* NOTE: cagney/2002-02-19: There was a test here to see if the
728b725ae77Skettenis 	 floating-point value was valid (using the macro
729b725ae77Skettenis 	 INVALID_FLOAT).  That test/macro have been removed.
730b725ae77Skettenis 
731b725ae77Skettenis 	 It turns out that only the VAX defined this macro and then
732b725ae77Skettenis 	 only in a non-portable way.  Fixing the portability problem
733b725ae77Skettenis 	 wouldn't help since the VAX floating-point code is also badly
734b725ae77Skettenis 	 bit-rotten.  The target needs to add definitions for the
735b725ae77Skettenis 	 methods TARGET_FLOAT_FORMAT and TARGET_DOUBLE_FORMAT - these
736b725ae77Skettenis 	 exactly describe the target floating-point format.  The
737b725ae77Skettenis 	 problem here is that the corresponding floatformat_vax_f and
738b725ae77Skettenis 	 floatformat_vax_d values these methods should be set to are
739b725ae77Skettenis 	 also not defined either.  Oops!
740b725ae77Skettenis 
741b725ae77Skettenis          Hopefully someone will add both the missing floatformat
742b725ae77Skettenis          definitions and the new cases for floatformat_is_valid ().  */
743b725ae77Skettenis 
744b725ae77Skettenis       if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
745e93f7393Sniklas 	{
746e93f7393Sniklas 	  *invp = 1;
747b725ae77Skettenis 	  return 0.0;
748e93f7393Sniklas 	}
749b725ae77Skettenis 
750b725ae77Skettenis       return extract_typed_floating (valaddr, type);
751e93f7393Sniklas     }
752e93f7393Sniklas   else if (nosign)
753e93f7393Sniklas     {
754e93f7393Sniklas       /* Unsigned -- be sure we compensate for signed LONGEST.  */
755b725ae77Skettenis       return (ULONGEST) unpack_long (type, valaddr);
756e93f7393Sniklas     }
757e93f7393Sniklas   else
758e93f7393Sniklas     {
759e93f7393Sniklas       /* Signed -- we are OK with unpack_long.  */
760e93f7393Sniklas       return unpack_long (type, valaddr);
761e93f7393Sniklas     }
762e93f7393Sniklas }
763e93f7393Sniklas 
764e93f7393Sniklas /* Unpack raw data (copied from debugee, target byte order) at VALADDR
765e93f7393Sniklas    as a CORE_ADDR, assuming the raw data is described by type TYPE.
766e93f7393Sniklas    We don't assume any alignment for the raw data.  Return value is in
767e93f7393Sniklas    host byte order.
768e93f7393Sniklas 
769e93f7393Sniklas    If you want functions and arrays to be coerced to pointers, and
770b725ae77Skettenis    references to be dereferenced, call value_as_address() instead.
771e93f7393Sniklas 
772e93f7393Sniklas    C++: It is assumed that the front-end has taken care of
773e93f7393Sniklas    all matters concerning pointers to members.  A pointer
774e93f7393Sniklas    to member which reaches here is considered to be equivalent
775e93f7393Sniklas    to an INT (or some size).  After all, it is only an offset.  */
776e93f7393Sniklas 
777e93f7393Sniklas CORE_ADDR
unpack_pointer(struct type * type,const char * valaddr)778b725ae77Skettenis unpack_pointer (struct type *type, const char *valaddr)
779e93f7393Sniklas {
780e93f7393Sniklas   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
781e93f7393Sniklas      whether we want this to be true eventually.  */
782e93f7393Sniklas   return unpack_long (type, valaddr);
783e93f7393Sniklas }
784b725ae77Skettenis 
785e93f7393Sniklas 
786b725ae77Skettenis /* Get the value of the FIELDN'th field (which must be static) of
787b725ae77Skettenis    TYPE.  Return NULL if the field doesn't exist or has been
788b725ae77Skettenis    optimized out. */
789b725ae77Skettenis 
790b725ae77Skettenis struct value *
value_static_field(struct type * type,int fieldno)791b725ae77Skettenis value_static_field (struct type *type, int fieldno)
792b725ae77Skettenis {
793b725ae77Skettenis   struct value *retval;
794b725ae77Skettenis 
795b725ae77Skettenis   if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno))
796b725ae77Skettenis     {
797b725ae77Skettenis       retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
798b725ae77Skettenis 			 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno),
799b725ae77Skettenis 			 NULL);
800b725ae77Skettenis     }
801b725ae77Skettenis   else
802b725ae77Skettenis     {
803b725ae77Skettenis       char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
804b725ae77Skettenis       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0, NULL);
805b725ae77Skettenis       if (sym == NULL)
806b725ae77Skettenis 	{
807b725ae77Skettenis 	  /* With some compilers, e.g. HP aCC, static data members are reported
808b725ae77Skettenis 	     as non-debuggable symbols */
809b725ae77Skettenis 	  struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
810b725ae77Skettenis 	  if (!msym)
811b725ae77Skettenis 	    return NULL;
812b725ae77Skettenis 	  else
813b725ae77Skettenis 	    {
814b725ae77Skettenis 	      retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
815b725ae77Skettenis 				 SYMBOL_VALUE_ADDRESS (msym),
816b725ae77Skettenis 				 SYMBOL_BFD_SECTION (msym));
817b725ae77Skettenis 	    }
818b725ae77Skettenis 	}
819b725ae77Skettenis       else
820b725ae77Skettenis 	{
821b725ae77Skettenis 	  /* SYM should never have a SYMBOL_CLASS which will require
822b725ae77Skettenis 	     read_var_value to use the FRAME parameter.  */
823b725ae77Skettenis 	  if (symbol_read_needs_frame (sym))
824b725ae77Skettenis 	    warning ("static field's value depends on the current "
825b725ae77Skettenis 		     "frame - bad debug info?");
826b725ae77Skettenis 	  retval = read_var_value (sym, NULL);
827b725ae77Skettenis  	}
828b725ae77Skettenis       if (retval && VALUE_LVAL (retval) == lval_memory)
829b725ae77Skettenis 	SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
830b725ae77Skettenis 			    VALUE_ADDRESS (retval));
831b725ae77Skettenis     }
832b725ae77Skettenis   return retval;
833b725ae77Skettenis }
834b725ae77Skettenis 
835b725ae77Skettenis /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
836b725ae77Skettenis    You have to be careful here, since the size of the data area for the value
837b725ae77Skettenis    is set by the length of the enclosing type.  So if NEW_ENCL_TYPE is bigger
838b725ae77Skettenis    than the old enclosing type, you have to allocate more space for the data.
839b725ae77Skettenis    The return value is a pointer to the new version of this value structure. */
840b725ae77Skettenis 
841b725ae77Skettenis struct value *
value_change_enclosing_type(struct value * val,struct type * new_encl_type)842b725ae77Skettenis value_change_enclosing_type (struct value *val, struct type *new_encl_type)
843b725ae77Skettenis {
844b725ae77Skettenis   if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)))
845b725ae77Skettenis     {
846b725ae77Skettenis       VALUE_ENCLOSING_TYPE (val) = new_encl_type;
847b725ae77Skettenis       return val;
848b725ae77Skettenis     }
849b725ae77Skettenis   else
850b725ae77Skettenis     {
851b725ae77Skettenis       struct value *new_val;
852b725ae77Skettenis       struct value *prev;
853b725ae77Skettenis 
854b725ae77Skettenis       new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type));
855b725ae77Skettenis 
856b725ae77Skettenis       VALUE_ENCLOSING_TYPE (new_val) = new_encl_type;
857b725ae77Skettenis 
858b725ae77Skettenis       /* We have to make sure this ends up in the same place in the value
859b725ae77Skettenis 	 chain as the original copy, so it's clean-up behavior is the same.
860b725ae77Skettenis 	 If the value has been released, this is a waste of time, but there
861b725ae77Skettenis 	 is no way to tell that in advance, so... */
862b725ae77Skettenis 
863b725ae77Skettenis       if (val != all_values)
864b725ae77Skettenis 	{
865b725ae77Skettenis 	  for (prev = all_values; prev != NULL; prev = prev->next)
866b725ae77Skettenis 	    {
867b725ae77Skettenis 	      if (prev->next == val)
868b725ae77Skettenis 		{
869b725ae77Skettenis 		  prev->next = new_val;
870b725ae77Skettenis 		  break;
871b725ae77Skettenis 		}
872b725ae77Skettenis 	    }
873b725ae77Skettenis 	}
874b725ae77Skettenis 
875b725ae77Skettenis       return new_val;
876b725ae77Skettenis     }
877b725ae77Skettenis }
878b725ae77Skettenis 
879e93f7393Sniklas /* Given a value ARG1 (offset by OFFSET bytes)
880e93f7393Sniklas    of a struct or union type ARG_TYPE,
881b725ae77Skettenis    extract and return the value of one of its (non-static) fields.
882b725ae77Skettenis    FIELDNO says which field. */
883e93f7393Sniklas 
884b725ae77Skettenis struct value *
value_primitive_field(struct value * arg1,int offset,int fieldno,struct type * arg_type)885b725ae77Skettenis value_primitive_field (struct value *arg1, int offset,
886b725ae77Skettenis 		       int fieldno, struct type *arg_type)
887e93f7393Sniklas {
888b725ae77Skettenis   struct value *v;
889b725ae77Skettenis   struct type *type;
890e93f7393Sniklas 
891e93f7393Sniklas   CHECK_TYPEDEF (arg_type);
892e93f7393Sniklas   type = TYPE_FIELD_TYPE (arg_type, fieldno);
893e93f7393Sniklas 
894e93f7393Sniklas   /* Handle packed fields */
895e93f7393Sniklas 
896e93f7393Sniklas   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
897e93f7393Sniklas     {
898e93f7393Sniklas       v = value_from_longest (type,
899e93f7393Sniklas 			      unpack_field_as_long (arg_type,
900b725ae77Skettenis 						    VALUE_CONTENTS (arg1)
901b725ae77Skettenis 						    + offset,
902e93f7393Sniklas 						    fieldno));
903e93f7393Sniklas       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
904e93f7393Sniklas       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
905b725ae77Skettenis       VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
906b725ae77Skettenis 	+ TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
907b725ae77Skettenis     }
908b725ae77Skettenis   else if (fieldno < TYPE_N_BASECLASSES (arg_type))
909b725ae77Skettenis     {
910b725ae77Skettenis       /* This field is actually a base subobject, so preserve the
911b725ae77Skettenis          entire object's contents for later references to virtual
912b725ae77Skettenis          bases, etc.  */
913b725ae77Skettenis       v = allocate_value (VALUE_ENCLOSING_TYPE (arg1));
914b725ae77Skettenis       VALUE_TYPE (v) = type;
915b725ae77Skettenis       if (VALUE_LAZY (arg1))
916b725ae77Skettenis 	VALUE_LAZY (v) = 1;
917b725ae77Skettenis       else
918b725ae77Skettenis 	memcpy (VALUE_CONTENTS_ALL_RAW (v), VALUE_CONTENTS_ALL_RAW (arg1),
919b725ae77Skettenis 		TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg1)));
920b725ae77Skettenis       VALUE_OFFSET (v) = VALUE_OFFSET (arg1);
921b725ae77Skettenis       VALUE_EMBEDDED_OFFSET (v)
922b725ae77Skettenis 	= offset +
923b725ae77Skettenis 	VALUE_EMBEDDED_OFFSET (arg1) +
924b725ae77Skettenis 	TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
925e93f7393Sniklas     }
926e93f7393Sniklas   else
927e93f7393Sniklas     {
928b725ae77Skettenis       /* Plain old data member */
929b725ae77Skettenis       offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
930e93f7393Sniklas       v = allocate_value (type);
931e93f7393Sniklas       if (VALUE_LAZY (arg1))
932e93f7393Sniklas 	VALUE_LAZY (v) = 1;
933e93f7393Sniklas       else
934b725ae77Skettenis 	memcpy (VALUE_CONTENTS_RAW (v),
935b725ae77Skettenis 		VALUE_CONTENTS_RAW (arg1) + offset,
936e93f7393Sniklas 		TYPE_LENGTH (type));
937b725ae77Skettenis       VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
938b725ae77Skettenis 			 + VALUE_EMBEDDED_OFFSET (arg1);
939e93f7393Sniklas     }
940e93f7393Sniklas   VALUE_LVAL (v) = VALUE_LVAL (arg1);
941e93f7393Sniklas   if (VALUE_LVAL (arg1) == lval_internalvar)
942e93f7393Sniklas     VALUE_LVAL (v) = lval_internalvar_component;
943e93f7393Sniklas   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
944b725ae77Skettenis   VALUE_REGNO (v) = VALUE_REGNO (arg1);
945b725ae77Skettenis /*  VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
946b725ae77Skettenis    + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; */
947e93f7393Sniklas   return v;
948e93f7393Sniklas }
949e93f7393Sniklas 
950e93f7393Sniklas /* Given a value ARG1 of a struct or union type,
951b725ae77Skettenis    extract and return the value of one of its (non-static) fields.
952b725ae77Skettenis    FIELDNO says which field. */
953e93f7393Sniklas 
954b725ae77Skettenis struct value *
value_field(struct value * arg1,int fieldno)955b725ae77Skettenis value_field (struct value *arg1, int fieldno)
956e93f7393Sniklas {
957e93f7393Sniklas   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
958e93f7393Sniklas }
959e93f7393Sniklas 
960e93f7393Sniklas /* Return a non-virtual function as a value.
961e93f7393Sniklas    F is the list of member functions which contains the desired method.
962b725ae77Skettenis    J is an index into F which provides the desired method.
963e93f7393Sniklas 
964b725ae77Skettenis    We only use the symbol for its address, so be happy with either a
965b725ae77Skettenis    full symbol or a minimal symbol.
966e93f7393Sniklas  */
967e93f7393Sniklas 
968b725ae77Skettenis struct value *
value_fn_field(struct value ** arg1p,struct fn_field * f,int j,struct type * type,int offset)969b725ae77Skettenis value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
970b725ae77Skettenis 		int offset)
971b725ae77Skettenis {
972b725ae77Skettenis   struct value *v;
973b725ae77Skettenis   struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
974b725ae77Skettenis   char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
975b725ae77Skettenis   struct symbol *sym;
976b725ae77Skettenis   struct minimal_symbol *msym;
977b725ae77Skettenis 
978b725ae77Skettenis   sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0, NULL);
979b725ae77Skettenis   if (sym != NULL)
980b725ae77Skettenis     {
981b725ae77Skettenis       msym = NULL;
982b725ae77Skettenis     }
983b725ae77Skettenis   else
984b725ae77Skettenis     {
985b725ae77Skettenis       gdb_assert (sym == NULL);
986b725ae77Skettenis       msym = lookup_minimal_symbol (physname, NULL, NULL);
987b725ae77Skettenis       if (msym == NULL)
988b725ae77Skettenis 	return NULL;
989b725ae77Skettenis     }
990b725ae77Skettenis 
991e93f7393Sniklas   v = allocate_value (ftype);
992b725ae77Skettenis   if (sym)
993b725ae77Skettenis     {
994e93f7393Sniklas       VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
995b725ae77Skettenis     }
996b725ae77Skettenis   else
997b725ae77Skettenis     {
998b725ae77Skettenis       VALUE_ADDRESS (v) = SYMBOL_VALUE_ADDRESS (msym);
999b725ae77Skettenis     }
1000e93f7393Sniklas 
1001e93f7393Sniklas   if (arg1p)
1002e93f7393Sniklas     {
1003e93f7393Sniklas       if (type != VALUE_TYPE (*arg1p))
1004e93f7393Sniklas 	*arg1p = value_ind (value_cast (lookup_pointer_type (type),
1005e93f7393Sniklas 					value_addr (*arg1p)));
1006e93f7393Sniklas 
1007e93f7393Sniklas       /* Move the `this' pointer according to the offset.
1008e93f7393Sniklas          VALUE_OFFSET (*arg1p) += offset;
1009e93f7393Sniklas        */
1010e93f7393Sniklas     }
1011e93f7393Sniklas 
1012e93f7393Sniklas   return v;
1013e93f7393Sniklas }
1014e93f7393Sniklas 
1015e93f7393Sniklas 
1016e93f7393Sniklas /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1017e93f7393Sniklas    VALADDR.
1018e93f7393Sniklas 
1019e93f7393Sniklas    Extracting bits depends on endianness of the machine.  Compute the
1020e93f7393Sniklas    number of least significant bits to discard.  For big endian machines,
1021e93f7393Sniklas    we compute the total number of bits in the anonymous object, subtract
1022e93f7393Sniklas    off the bit count from the MSB of the object to the MSB of the
1023e93f7393Sniklas    bitfield, then the size of the bitfield, which leaves the LSB discard
1024e93f7393Sniklas    count.  For little endian machines, the discard count is simply the
1025e93f7393Sniklas    number of bits from the LSB of the anonymous object to the LSB of the
1026e93f7393Sniklas    bitfield.
1027e93f7393Sniklas 
1028e93f7393Sniklas    If the field is signed, we also do sign extension. */
1029e93f7393Sniklas 
1030e93f7393Sniklas LONGEST
unpack_field_as_long(struct type * type,const char * valaddr,int fieldno)1031b725ae77Skettenis unpack_field_as_long (struct type *type, const char *valaddr, int fieldno)
1032e93f7393Sniklas {
1033b725ae77Skettenis   ULONGEST val;
1034b725ae77Skettenis   ULONGEST valmask;
1035e93f7393Sniklas   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1036e93f7393Sniklas   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1037e93f7393Sniklas   int lsbcount;
1038b725ae77Skettenis   struct type *field_type;
1039e93f7393Sniklas 
1040e93f7393Sniklas   val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1041b725ae77Skettenis   field_type = TYPE_FIELD_TYPE (type, fieldno);
1042b725ae77Skettenis   CHECK_TYPEDEF (field_type);
1043e93f7393Sniklas 
1044e93f7393Sniklas   /* Extract bits.  See comment above. */
1045e93f7393Sniklas 
1046e93f7393Sniklas   if (BITS_BIG_ENDIAN)
1047e93f7393Sniklas     lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1048e93f7393Sniklas   else
1049e93f7393Sniklas     lsbcount = (bitpos % 8);
1050e93f7393Sniklas   val >>= lsbcount;
1051e93f7393Sniklas 
1052e93f7393Sniklas   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1053e93f7393Sniklas      If the field is signed, and is negative, then sign extend. */
1054e93f7393Sniklas 
1055e93f7393Sniklas   if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
1056e93f7393Sniklas     {
1057b725ae77Skettenis       valmask = (((ULONGEST) 1) << bitsize) - 1;
1058e93f7393Sniklas       val &= valmask;
1059b725ae77Skettenis       if (!TYPE_UNSIGNED (field_type))
1060e93f7393Sniklas 	{
1061e93f7393Sniklas 	  if (val & (valmask ^ (valmask >> 1)))
1062e93f7393Sniklas 	    {
1063e93f7393Sniklas 	      val |= ~valmask;
1064e93f7393Sniklas 	    }
1065e93f7393Sniklas 	}
1066e93f7393Sniklas     }
1067e93f7393Sniklas   return (val);
1068e93f7393Sniklas }
1069e93f7393Sniklas 
1070e93f7393Sniklas /* Modify the value of a bitfield.  ADDR points to a block of memory in
1071e93f7393Sniklas    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
1072e93f7393Sniklas    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
1073e93f7393Sniklas    indicate which bits (in target bit order) comprise the bitfield.  */
1074e93f7393Sniklas 
1075e93f7393Sniklas void
modify_field(char * addr,LONGEST fieldval,int bitpos,int bitsize)1076b725ae77Skettenis modify_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
1077e93f7393Sniklas {
1078e93f7393Sniklas   LONGEST oword;
1079e93f7393Sniklas 
1080e93f7393Sniklas   /* If a negative fieldval fits in the field in question, chop
1081e93f7393Sniklas      off the sign extension bits.  */
1082e93f7393Sniklas   if (bitsize < (8 * (int) sizeof (fieldval))
1083e93f7393Sniklas       && (~fieldval & ~((1 << (bitsize - 1)) - 1)) == 0)
1084e93f7393Sniklas     fieldval = fieldval & ((1 << bitsize) - 1);
1085e93f7393Sniklas 
1086e93f7393Sniklas   /* Warn if value is too big to fit in the field in question.  */
1087e93f7393Sniklas   if (bitsize < (8 * (int) sizeof (fieldval))
1088e93f7393Sniklas       && 0 != (fieldval & ~((1 << bitsize) - 1)))
1089e93f7393Sniklas     {
1090e93f7393Sniklas       /* FIXME: would like to include fieldval in the message, but
1091e93f7393Sniklas          we don't have a sprintf_longest.  */
1092e93f7393Sniklas       warning ("Value does not fit in %d bits.", bitsize);
1093e93f7393Sniklas 
1094e93f7393Sniklas       /* Truncate it, otherwise adjoining fields may be corrupted.  */
1095e93f7393Sniklas       fieldval = fieldval & ((1 << bitsize) - 1);
1096e93f7393Sniklas     }
1097e93f7393Sniklas 
1098e93f7393Sniklas   oword = extract_signed_integer (addr, sizeof oword);
1099e93f7393Sniklas 
1100e93f7393Sniklas   /* Shifting for bit field depends on endianness of the target machine.  */
1101e93f7393Sniklas   if (BITS_BIG_ENDIAN)
1102e93f7393Sniklas     bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1103e93f7393Sniklas 
1104e93f7393Sniklas   /* Mask out old value, while avoiding shifts >= size of oword */
1105e93f7393Sniklas   if (bitsize < 8 * (int) sizeof (oword))
1106b725ae77Skettenis     oword &= ~(((((ULONGEST) 1) << bitsize) - 1) << bitpos);
1107e93f7393Sniklas   else
1108b725ae77Skettenis     oword &= ~((~(ULONGEST) 0) << bitpos);
1109e93f7393Sniklas   oword |= fieldval << bitpos;
1110e93f7393Sniklas 
1111e93f7393Sniklas   store_signed_integer (addr, sizeof oword, oword);
1112e93f7393Sniklas }
1113e93f7393Sniklas 
1114e93f7393Sniklas /* Convert C numbers into newly allocated values */
1115e93f7393Sniklas 
1116b725ae77Skettenis struct value *
value_from_longest(struct type * type,LONGEST num)1117b725ae77Skettenis value_from_longest (struct type *type, LONGEST num)
1118e93f7393Sniklas {
1119b725ae77Skettenis   struct value *val = allocate_value (type);
1120b725ae77Skettenis   enum type_code code;
1121b725ae77Skettenis   int len;
1122e93f7393Sniklas retry:
1123e93f7393Sniklas   code = TYPE_CODE (type);
1124e93f7393Sniklas   len = TYPE_LENGTH (type);
1125e93f7393Sniklas 
1126e93f7393Sniklas   switch (code)
1127e93f7393Sniklas     {
1128e93f7393Sniklas     case TYPE_CODE_TYPEDEF:
1129e93f7393Sniklas       type = check_typedef (type);
1130e93f7393Sniklas       goto retry;
1131e93f7393Sniklas     case TYPE_CODE_INT:
1132e93f7393Sniklas     case TYPE_CODE_CHAR:
1133e93f7393Sniklas     case TYPE_CODE_ENUM:
1134e93f7393Sniklas     case TYPE_CODE_BOOL:
1135e93f7393Sniklas     case TYPE_CODE_RANGE:
1136e93f7393Sniklas       store_signed_integer (VALUE_CONTENTS_RAW (val), len, num);
1137e93f7393Sniklas       break;
1138e93f7393Sniklas 
1139e93f7393Sniklas     case TYPE_CODE_REF:
1140e93f7393Sniklas     case TYPE_CODE_PTR:
1141b725ae77Skettenis       store_typed_address (VALUE_CONTENTS_RAW (val), type, (CORE_ADDR) num);
1142e93f7393Sniklas       break;
1143e93f7393Sniklas 
1144e93f7393Sniklas     default:
1145b725ae77Skettenis       error ("Unexpected type (%d) encountered for integer constant.", code);
1146e93f7393Sniklas     }
1147e93f7393Sniklas   return val;
1148e93f7393Sniklas }
1149e93f7393Sniklas 
1150b725ae77Skettenis 
1151b725ae77Skettenis /* Create a value representing a pointer of type TYPE to the address
1152b725ae77Skettenis    ADDR.  */
1153b725ae77Skettenis struct value *
value_from_pointer(struct type * type,CORE_ADDR addr)1154b725ae77Skettenis value_from_pointer (struct type *type, CORE_ADDR addr)
1155e93f7393Sniklas {
1156b725ae77Skettenis   struct value *val = allocate_value (type);
1157b725ae77Skettenis   store_typed_address (VALUE_CONTENTS_RAW (val), type, addr);
1158b725ae77Skettenis   return val;
1159b725ae77Skettenis }
1160b725ae77Skettenis 
1161b725ae77Skettenis 
1162b725ae77Skettenis /* Create a value for a string constant to be stored locally
1163b725ae77Skettenis    (not in the inferior's memory space, but in GDB memory).
1164b725ae77Skettenis    This is analogous to value_from_longest, which also does not
1165b725ae77Skettenis    use inferior memory.  String shall NOT contain embedded nulls.  */
1166b725ae77Skettenis 
1167b725ae77Skettenis struct value *
value_from_string(char * ptr)1168b725ae77Skettenis value_from_string (char *ptr)
1169b725ae77Skettenis {
1170b725ae77Skettenis   struct value *val;
1171b725ae77Skettenis   int len = strlen (ptr);
1172b725ae77Skettenis   int lowbound = current_language->string_lower_bound;
1173*63addd46Skettenis   struct type *string_char_type;
1174*63addd46Skettenis   struct type *rangetype;
1175*63addd46Skettenis   struct type *stringtype;
1176*63addd46Skettenis 
1177*63addd46Skettenis   rangetype = create_range_type ((struct type *) NULL,
1178b725ae77Skettenis 				 builtin_type_int,
1179b725ae77Skettenis 				 lowbound, len + lowbound - 1);
1180*63addd46Skettenis   string_char_type = language_string_char_type (current_language,
1181*63addd46Skettenis 						current_gdbarch);
1182*63addd46Skettenis   stringtype = create_array_type ((struct type *) NULL,
1183*63addd46Skettenis 				  string_char_type,
1184b725ae77Skettenis 				  rangetype);
1185b725ae77Skettenis   val = allocate_value (stringtype);
1186b725ae77Skettenis   memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1187b725ae77Skettenis   return val;
1188b725ae77Skettenis }
1189b725ae77Skettenis 
1190b725ae77Skettenis struct value *
value_from_double(struct type * type,DOUBLEST num)1191b725ae77Skettenis value_from_double (struct type *type, DOUBLEST num)
1192b725ae77Skettenis {
1193b725ae77Skettenis   struct value *val = allocate_value (type);
1194e93f7393Sniklas   struct type *base_type = check_typedef (type);
1195b725ae77Skettenis   enum type_code code = TYPE_CODE (base_type);
1196b725ae77Skettenis   int len = TYPE_LENGTH (base_type);
1197e93f7393Sniklas 
1198e93f7393Sniklas   if (code == TYPE_CODE_FLT)
1199e93f7393Sniklas     {
1200b725ae77Skettenis       store_typed_floating (VALUE_CONTENTS_RAW (val), base_type, num);
1201e93f7393Sniklas     }
1202e93f7393Sniklas   else
1203e93f7393Sniklas     error ("Unexpected type encountered for floating constant.");
1204e93f7393Sniklas 
1205e93f7393Sniklas   return val;
1206e93f7393Sniklas }
1207e93f7393Sniklas 
1208b725ae77Skettenis 
1209b725ae77Skettenis /* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of
1210b725ae77Skettenis    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc and TYPE
1211b725ae77Skettenis    is the type (which is known to be struct, union or array).
1212e93f7393Sniklas 
1213e93f7393Sniklas    On most machines, the struct convention is used unless we are
1214e93f7393Sniklas    using gcc and the type is of a special size.  */
1215e93f7393Sniklas /* As of about 31 Mar 93, GCC was changed to be compatible with the
1216e93f7393Sniklas    native compiler.  GCC 2.3.3 was the last release that did it the
1217e93f7393Sniklas    old way.  Since gcc2_compiled was not changed, we have no
1218e93f7393Sniklas    way to correctly win in all cases, so we just do the right thing
1219e93f7393Sniklas    for gcc1 and for gcc2 after this change.  Thus it loses for gcc
1220e93f7393Sniklas    2.0-2.3.3.  This is somewhat unfortunate, but changing gcc2_compiled
1221e93f7393Sniklas    would cause more chaos than dealing with some struct returns being
1222e93f7393Sniklas    handled wrong.  */
1223*63addd46Skettenis /* NOTE: cagney/2004-06-13: Deleted check for "gcc_p".  GCC 1.x is
1224*63addd46Skettenis    dead.  */
1225e93f7393Sniklas 
1226b725ae77Skettenis int
generic_use_struct_convention(int gcc_p,struct type * value_type)1227b725ae77Skettenis generic_use_struct_convention (int gcc_p, struct type *value_type)
1228b725ae77Skettenis {
1229*63addd46Skettenis   return !(TYPE_LENGTH (value_type) == 1
1230b725ae77Skettenis 	   || TYPE_LENGTH (value_type) == 2
1231b725ae77Skettenis 	   || TYPE_LENGTH (value_type) == 4
1232*63addd46Skettenis 	   || TYPE_LENGTH (value_type) == 8);
1233b725ae77Skettenis }
1234e93f7393Sniklas 
1235b725ae77Skettenis /* Return true if the function returning the specified type is using
1236b725ae77Skettenis    the convention of returning structures in memory (passing in the
1237b725ae77Skettenis    address as a hidden first parameter).  GCC_P is nonzero if compiled
1238e93f7393Sniklas    with GCC.  */
1239e93f7393Sniklas 
1240e93f7393Sniklas int
using_struct_return(struct type * value_type,int gcc_p)1241b725ae77Skettenis using_struct_return (struct type *value_type, int gcc_p)
1242e93f7393Sniklas {
1243b725ae77Skettenis   enum type_code code = TYPE_CODE (value_type);
1244e93f7393Sniklas 
1245e93f7393Sniklas   if (code == TYPE_CODE_ERROR)
1246e93f7393Sniklas     error ("Function return type unknown.");
1247e93f7393Sniklas 
1248b725ae77Skettenis   if (code == TYPE_CODE_VOID)
1249b725ae77Skettenis     /* A void return value is never in memory.  See also corresponding
1250*63addd46Skettenis        code in "print_return_value".  */
1251b725ae77Skettenis     return 0;
1252b725ae77Skettenis 
1253b725ae77Skettenis   /* Probe the architecture for the return-value convention.  */
1254b725ae77Skettenis   return (gdbarch_return_value (current_gdbarch, value_type,
1255b725ae77Skettenis 				NULL, NULL, NULL)
1256*63addd46Skettenis 	  != RETURN_VALUE_REGISTER_CONVENTION);
1257e93f7393Sniklas }
1258b725ae77Skettenis 
1259e93f7393Sniklas void
_initialize_values(void)1260b725ae77Skettenis _initialize_values (void)
1261e93f7393Sniklas {
1262e93f7393Sniklas   add_cmd ("convenience", no_class, show_convenience,
1263e93f7393Sniklas 	   "Debugger convenience (\"$foo\") variables.\n\
1264e93f7393Sniklas These variables are created when you assign them values;\n\
1265e93f7393Sniklas thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
1266e93f7393Sniklas A few convenience variables are given values automatically:\n\
1267e93f7393Sniklas \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1268e93f7393Sniklas \"$__\" holds the contents of the last address examined with \"x\".",
1269e93f7393Sniklas 	   &showlist);
1270e93f7393Sniklas 
1271e93f7393Sniklas   add_cmd ("values", no_class, show_values,
1272e93f7393Sniklas 	   "Elements of value history around item number IDX (or last ten).",
1273e93f7393Sniklas 	   &showlist);
1274e93f7393Sniklas }
1275