* values.c allocate_repeat_value): Allocate an array type, and
[external/binutils.git] / gdb / values.c
1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
2    Copyright 1986, 1987, 1989, 1991, 1993, 1994, 1995
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "frame.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "target.h"
31 #include "language.h"
32 #include "demangle.h"
33
34 /* Local function prototypes. */
35
36 static value_ptr value_headof PARAMS ((value_ptr, struct type *,
37                                        struct type *));
38
39 static void show_values PARAMS ((char *, int));
40
41 static void show_convenience PARAMS ((char *, int));
42
43 /* The value-history records all the values printed
44    by print commands during this session.  Each chunk
45    records 60 consecutive values.  The first chunk on
46    the chain records the most recent values.
47    The total number of values is in value_history_count.  */
48
49 #define VALUE_HISTORY_CHUNK 60
50
51 struct value_history_chunk
52 {
53   struct value_history_chunk *next;
54   value_ptr values[VALUE_HISTORY_CHUNK];
55 };
56
57 /* Chain of chunks now in use.  */
58
59 static struct value_history_chunk *value_history_chain;
60
61 static int value_history_count; /* Abs number of last entry stored */
62 \f
63 /* List of all value objects currently allocated
64    (except for those released by calls to release_value)
65    This is so they can be freed after each command.  */
66
67 static value_ptr all_values;
68
69 /* Allocate a  value  that has the correct length for type TYPE.  */
70
71 value_ptr
72 allocate_value (type)
73      struct type *type;
74 {
75   register value_ptr val;
76
77   check_stub_type (type);
78
79   val = (struct value *) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
80   VALUE_NEXT (val) = all_values;
81   all_values = val;
82   VALUE_TYPE (val) = type;
83   VALUE_LVAL (val) = not_lval;
84   VALUE_ADDRESS (val) = 0;
85   VALUE_FRAME (val) = 0;
86   VALUE_OFFSET (val) = 0;
87   VALUE_BITPOS (val) = 0;
88   VALUE_BITSIZE (val) = 0;
89   VALUE_REGNO (val) = -1;
90   VALUE_LAZY (val) = 0;
91   VALUE_OPTIMIZED_OUT (val) = 0;
92   val->modifiable = 1;
93   return val;
94 }
95
96 /* Allocate a  value  that has the correct length
97    for COUNT repetitions type TYPE.  */
98
99 value_ptr
100 allocate_repeat_value (type, count)
101      struct type *type;
102      int count;
103 {
104   struct type *element_type = type;
105   int low_bound = current_language->string_lower_bound; /* ??? */
106   /* FIXME-type-allocation: need a way to free this type when we are
107      done with it.  */
108   struct type *range_type
109     = create_range_type ((struct type *) NULL, builtin_type_int,
110                          low_bound, count + low_bound - 1);
111   /* FIXME-type-allocation: need a way to free this type when we are
112      done with it.  */
113   return allocate_value (create_array_type ((struct type *) NULL,
114                                             type, range_type));
115 }
116
117 /* Return a mark in the value chain.  All values allocated after the
118    mark is obtained (except for those released) are subject to being freed
119    if a subsequent value_free_to_mark is passed the mark.  */
120 value_ptr
121 value_mark ()
122 {
123   return all_values;
124 }
125
126 /* Free all values allocated since MARK was obtained by value_mark
127    (except for those released).  */
128 void
129 value_free_to_mark (mark)
130      value_ptr mark;
131 {
132   value_ptr val, next;
133
134   for (val = all_values; val && val != mark; val = next)
135     {
136       next = VALUE_NEXT (val);
137       value_free (val);
138     }
139   all_values = val;
140 }
141
142 /* Free all the values that have been allocated (except for those released).
143    Called after each command, successful or not.  */
144
145 void
146 free_all_values ()
147 {
148   register value_ptr val, next;
149
150   for (val = all_values; val; val = next)
151     {
152       next = VALUE_NEXT (val);
153       value_free (val);
154     }
155
156   all_values = 0;
157 }
158
159 /* Remove VAL from the chain all_values
160    so it will not be freed automatically.  */
161
162 void
163 release_value (val)
164      register value_ptr val;
165 {
166   register value_ptr v;
167
168   if (all_values == val)
169     {
170       all_values = val->next;
171       return;
172     }
173
174   for (v = all_values; v; v = v->next)
175     {
176       if (v->next == val)
177         {
178           v->next = val->next;
179           break;
180         }
181     }
182 }
183
184 /* Release all values up to mark  */
185 value_ptr
186 value_release_to_mark (mark)
187      value_ptr mark;
188 {
189   value_ptr val, next;
190
191   for (val = next = all_values; next; next = VALUE_NEXT (next))
192     if (VALUE_NEXT (next) == mark)
193       {
194         all_values = VALUE_NEXT (next);
195         VALUE_NEXT (next) = 0;
196         return val;
197       }
198   all_values = 0;
199   return val;
200 }
201
202 /* Return a copy of the value ARG.
203    It contains the same contents, for same memory address,
204    but it's a different block of storage.  */
205
206 value_ptr
207 value_copy (arg)
208      value_ptr arg;
209 {
210   register struct type *type = VALUE_TYPE (arg);
211   register value_ptr val = allocate_value (type);
212   VALUE_LVAL (val) = VALUE_LVAL (arg);
213   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
214   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
215   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
216   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
217   VALUE_FRAME (val) = VALUE_FRAME (arg);
218   VALUE_REGNO (val) = VALUE_REGNO (arg);
219   VALUE_LAZY (val) = VALUE_LAZY (arg);
220   VALUE_OPTIMIZED_OUT (val) = VALUE_OPTIMIZED_OUT (arg);
221   val->modifiable = arg->modifiable;
222   if (!VALUE_LAZY (val))
223     {
224       memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS_RAW (arg),
225               TYPE_LENGTH (VALUE_TYPE (arg)));
226     }
227   return val;
228 }
229 \f
230 /* Access to the value history.  */
231
232 /* Record a new value in the value history.
233    Returns the absolute history index of the entry.
234    Result of -1 indicates the value was not saved; otherwise it is the
235    value history index of this new item.  */
236
237 int
238 record_latest_value (val)
239      value_ptr val;
240 {
241   int i;
242
243   /* Check error now if about to store an invalid float.  We return -1
244      to the caller, but allow them to continue, e.g. to print it as "Nan". */
245   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT)
246     {
247       unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
248       if (i) return -1;         /* Indicate value not saved in history */
249     }
250
251   /* We don't want this value to have anything to do with the inferior anymore.
252      In particular, "set $1 = 50" should not affect the variable from which
253      the value was taken, and fast watchpoints should be able to assume that
254      a value on the value history never changes.  */
255   if (VALUE_LAZY (val))
256     value_fetch_lazy (val);
257   /* We preserve VALUE_LVAL so that the user can find out where it was fetched
258      from.  This is a bit dubious, because then *&$1 does not just return $1
259      but the current contents of that location.  c'est la vie...  */
260   val->modifiable = 0;
261   release_value (val);
262
263   /* Here we treat value_history_count as origin-zero
264      and applying to the value being stored now.  */
265
266   i = value_history_count % VALUE_HISTORY_CHUNK;
267   if (i == 0)
268     {
269       register struct value_history_chunk *new
270         = (struct value_history_chunk *)
271           xmalloc (sizeof (struct value_history_chunk));
272       memset (new->values, 0, sizeof new->values);
273       new->next = value_history_chain;
274       value_history_chain = new;
275     }
276
277   value_history_chain->values[i] = val;
278
279   /* Now we regard value_history_count as origin-one
280      and applying to the value just stored.  */
281
282   return ++value_history_count;
283 }
284
285 /* Return a copy of the value in the history with sequence number NUM.  */
286
287 value_ptr
288 access_value_history (num)
289      int num;
290 {
291   register struct value_history_chunk *chunk;
292   register int i;
293   register int absnum = num;
294
295   if (absnum <= 0)
296     absnum += value_history_count;
297
298   if (absnum <= 0)
299     {
300       if (num == 0)
301         error ("The history is empty.");
302       else if (num == 1)
303         error ("There is only one value in the history.");
304       else
305         error ("History does not go back to $$%d.", -num);
306     }
307   if (absnum > value_history_count)
308     error ("History has not yet reached $%d.", absnum);
309
310   absnum--;
311
312   /* Now absnum is always absolute and origin zero.  */
313
314   chunk = value_history_chain;
315   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
316        i > 0; i--)
317     chunk = chunk->next;
318
319   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
320 }
321
322 /* Clear the value history entirely.
323    Must be done when new symbol tables are loaded,
324    because the type pointers become invalid.  */
325
326 void
327 clear_value_history ()
328 {
329   register struct value_history_chunk *next;
330   register int i;
331   register value_ptr val;
332
333   while (value_history_chain)
334     {
335       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
336         if ((val = value_history_chain->values[i]) != NULL)
337           free ((PTR)val);
338       next = value_history_chain->next;
339       free ((PTR)value_history_chain);
340       value_history_chain = next;
341     }
342   value_history_count = 0;
343 }
344
345 static void
346 show_values (num_exp, from_tty)
347      char *num_exp;
348      int from_tty;
349 {
350   register int i;
351   register value_ptr val;
352   static int num = 1;
353
354   if (num_exp)
355     {
356         /* "info history +" should print from the stored position.
357            "info history <exp>" should print around value number <exp>.  */
358       if (num_exp[0] != '+' || num_exp[1] != '\0')
359         num = parse_and_eval_address (num_exp) - 5;
360     }
361   else
362     {
363       /* "info history" means print the last 10 values.  */
364       num = value_history_count - 9;
365     }
366
367   if (num <= 0)
368     num = 1;
369
370   for (i = num; i < num + 10 && i <= value_history_count; i++)
371     {
372       val = access_value_history (i);
373       printf_filtered ("$%d = ", i);
374       value_print (val, gdb_stdout, 0, Val_pretty_default);
375       printf_filtered ("\n");
376     }
377
378   /* The next "info history +" should start after what we just printed.  */
379   num += 10;
380
381   /* Hitting just return after this command should do the same thing as
382      "info history +".  If num_exp is null, this is unnecessary, since
383      "info history +" is not useful after "info history".  */
384   if (from_tty && num_exp)
385     {
386       num_exp[0] = '+';
387       num_exp[1] = '\0';
388     }
389 }
390 \f
391 /* Internal variables.  These are variables within the debugger
392    that hold values assigned by debugger commands.
393    The user refers to them with a '$' prefix
394    that does not appear in the variable names stored internally.  */
395
396 static struct internalvar *internalvars;
397
398 /* Look up an internal variable with name NAME.  NAME should not
399    normally include a dollar sign.
400
401    If the specified internal variable does not exist,
402    one is created, with a void value.  */
403
404 struct internalvar *
405 lookup_internalvar (name)
406      char *name;
407 {
408   register struct internalvar *var;
409
410   for (var = internalvars; var; var = var->next)
411     if (STREQ (var->name, name))
412       return var;
413
414   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
415   var->name = concat (name, NULL);
416   var->value = allocate_value (builtin_type_void);
417   release_value (var->value);
418   var->next = internalvars;
419   internalvars = var;
420   return var;
421 }
422
423 value_ptr
424 value_of_internalvar (var)
425      struct internalvar *var;
426 {
427   register value_ptr val;
428
429 #ifdef IS_TRAPPED_INTERNALVAR
430   if (IS_TRAPPED_INTERNALVAR (var->name))
431     return VALUE_OF_TRAPPED_INTERNALVAR (var);
432 #endif 
433
434   val = value_copy (var->value);
435   if (VALUE_LAZY (val))
436     value_fetch_lazy (val);
437   VALUE_LVAL (val) = lval_internalvar;
438   VALUE_INTERNALVAR (val) = var;
439   return val;
440 }
441
442 void
443 set_internalvar_component (var, offset, bitpos, bitsize, newval)
444      struct internalvar *var;
445      int offset, bitpos, bitsize;
446      value_ptr newval;
447 {
448   register char *addr = VALUE_CONTENTS (var->value) + offset;
449
450 #ifdef IS_TRAPPED_INTERNALVAR
451   if (IS_TRAPPED_INTERNALVAR (var->name))
452     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
453 #endif
454
455   if (bitsize)
456     modify_field (addr, value_as_long (newval),
457                   bitpos, bitsize);
458   else
459     memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval)));
460 }
461
462 void
463 set_internalvar (var, val)
464      struct internalvar *var;
465      value_ptr val;
466 {
467   value_ptr newval;
468
469 #ifdef IS_TRAPPED_INTERNALVAR
470   if (IS_TRAPPED_INTERNALVAR (var->name))
471     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
472 #endif
473
474   newval = value_copy (val);
475   newval->modifiable = 1;
476
477   /* Force the value to be fetched from the target now, to avoid problems
478      later when this internalvar is referenced and the target is gone or
479      has changed.  */
480   if (VALUE_LAZY (newval))
481     value_fetch_lazy (newval);
482
483   /* Begin code which must not call error().  If var->value points to
484      something free'd, an error() obviously leaves a dangling pointer.
485      But we also get a danling pointer if var->value points to
486      something in the value chain (i.e., before release_value is
487      called), because after the error free_all_values will get called before
488      long.  */
489   free ((PTR)var->value);
490   var->value = newval;
491   release_value (newval);
492   /* End code which must not call error().  */
493 }
494
495 char *
496 internalvar_name (var)
497      struct internalvar *var;
498 {
499   return var->name;
500 }
501
502 /* Free all internalvars.  Done when new symtabs are loaded,
503    because that makes the values invalid.  */
504
505 void
506 clear_internalvars ()
507 {
508   register struct internalvar *var;
509
510   while (internalvars)
511     {
512       var = internalvars;
513       internalvars = var->next;
514       free ((PTR)var->name);
515       free ((PTR)var->value);
516       free ((PTR)var);
517     }
518 }
519
520 static void
521 show_convenience (ignore, from_tty)
522      char *ignore;
523      int from_tty;
524 {
525   register struct internalvar *var;
526   int varseen = 0;
527
528   for (var = internalvars; var; var = var->next)
529     {
530 #ifdef IS_TRAPPED_INTERNALVAR
531       if (IS_TRAPPED_INTERNALVAR (var->name))
532         continue;
533 #endif
534       if (!varseen)
535         {
536           varseen = 1;
537         }
538       printf_filtered ("$%s = ", var->name);
539       value_print (var->value, gdb_stdout, 0, Val_pretty_default);
540       printf_filtered ("\n");
541     }
542   if (!varseen)
543     printf_unfiltered ("No debugger convenience variables now defined.\n\
544 Convenience variables have names starting with \"$\";\n\
545 use \"set\" as in \"set $foo = 5\" to define them.\n");
546 }
547 \f
548 /* Extract a value as a C number (either long or double).
549    Knows how to convert fixed values to double, or
550    floating values to long.
551    Does not deallocate the value.  */
552
553 LONGEST
554 value_as_long (val)
555      register value_ptr val;
556 {
557   /* This coerces arrays and functions, which is necessary (e.g.
558      in disassemble_command).  It also dereferences references, which
559      I suspect is the most logical thing to do.  */
560   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
561     COERCE_ARRAY (val);
562   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
563 }
564
565 double
566 value_as_double (val)
567      register value_ptr val;
568 {
569   double foo;
570   int inv;
571   
572   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
573   if (inv)
574     error ("Invalid floating value found in program.");
575   return foo;
576 }
577 /* Extract a value as a C pointer.
578    Does not deallocate the value.  */
579 CORE_ADDR
580 value_as_pointer (val)
581      value_ptr val;
582 {
583   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
584      whether we want this to be true eventually.  */
585 #if 0
586   /* ADDR_BITS_REMOVE is wrong if we are being called for a
587      non-address (e.g. argument to "signal", "info break", etc.), or
588      for pointers to char, in which the low bits *are* significant.  */
589   return ADDR_BITS_REMOVE(value_as_long (val));
590 #else
591   return value_as_long (val);
592 #endif
593 }
594 \f
595 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
596    as a long, or as a double, assuming the raw data is described
597    by type TYPE.  Knows how to convert different sizes of values
598    and can convert between fixed and floating point.  We don't assume
599    any alignment for the raw data.  Return value is in host byte order.
600
601    If you want functions and arrays to be coerced to pointers, and
602    references to be dereferenced, call value_as_long() instead.
603
604    C++: It is assumed that the front-end has taken care of
605    all matters concerning pointers to members.  A pointer
606    to member which reaches here is considered to be equivalent
607    to an INT (or some size).  After all, it is only an offset.  */
608
609 LONGEST
610 unpack_long (type, valaddr)
611      struct type *type;
612      char *valaddr;
613 {
614   register enum type_code code = TYPE_CODE (type);
615   register int len = TYPE_LENGTH (type);
616   register int nosign = TYPE_UNSIGNED (type);
617
618   if (current_language->la_language == language_scm
619       && is_scmvalue_type (type))
620     return scm_unpack (type, valaddr, TYPE_CODE_INT);
621
622   switch (code)
623     {
624     case TYPE_CODE_ENUM:
625     case TYPE_CODE_BOOL:
626     case TYPE_CODE_INT:
627     case TYPE_CODE_CHAR:
628     case TYPE_CODE_RANGE:
629       if (nosign)
630         return extract_unsigned_integer (valaddr, len);
631       else
632         return extract_signed_integer (valaddr, len);
633
634     case TYPE_CODE_FLT:
635       return extract_floating (valaddr, len);
636
637     case TYPE_CODE_PTR:
638     case TYPE_CODE_REF:
639       /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
640          whether we want this to be true eventually.  */
641       return extract_address (valaddr, len);
642
643     case TYPE_CODE_MEMBER:
644       error ("not implemented: member types in unpack_long");
645
646     default:
647       error ("Value can't be converted to integer.");
648     }
649   return 0; /* Placate lint.  */
650 }
651
652 /* Return a double value from the specified type and address.
653    INVP points to an int which is set to 0 for valid value,
654    1 for invalid value (bad float format).  In either case,
655    the returned double is OK to use.  Argument is in target
656    format, result is in host format.  */
657
658 double
659 unpack_double (type, valaddr, invp)
660      struct type *type;
661      char *valaddr;
662      int *invp;
663 {
664   register enum type_code code = TYPE_CODE (type);
665   register int len = TYPE_LENGTH (type);
666   register int nosign = TYPE_UNSIGNED (type);
667
668   *invp = 0;                    /* Assume valid.   */
669   if (code == TYPE_CODE_FLT)
670     {
671 #ifdef INVALID_FLOAT
672       if (INVALID_FLOAT (valaddr, len))
673         {
674           *invp = 1;
675           return 1.234567891011121314;
676         }
677 #endif
678       return extract_floating (valaddr, len);
679     }
680   else if (nosign)
681     {
682       /* Unsigned -- be sure we compensate for signed LONGEST.  */
683       return (unsigned LONGEST) unpack_long (type, valaddr);
684     }
685   else
686     {
687       /* Signed -- we are OK with unpack_long.  */
688       return unpack_long (type, valaddr);
689     }
690 }
691
692 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
693    as a CORE_ADDR, assuming the raw data is described by type TYPE.
694    We don't assume any alignment for the raw data.  Return value is in
695    host byte order.
696
697    If you want functions and arrays to be coerced to pointers, and
698    references to be dereferenced, call value_as_pointer() instead.
699
700    C++: It is assumed that the front-end has taken care of
701    all matters concerning pointers to members.  A pointer
702    to member which reaches here is considered to be equivalent
703    to an INT (or some size).  After all, it is only an offset.  */
704
705 CORE_ADDR
706 unpack_pointer (type, valaddr)
707      struct type *type;
708      char *valaddr;
709 {
710   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
711      whether we want this to be true eventually.  */
712   return unpack_long (type, valaddr);
713 }
714 \f
715 /* Given a value ARG1 (offset by OFFSET bytes)
716    of a struct or union type ARG_TYPE,
717    extract and return the value of one of its fields.
718    FIELDNO says which field.
719
720    For C++, must also be able to return values from static fields */
721
722 value_ptr
723 value_primitive_field (arg1, offset, fieldno, arg_type)
724      register value_ptr arg1;
725      int offset;
726      register int fieldno;
727      register struct type *arg_type;
728 {
729   register value_ptr v;
730   register struct type *type;
731
732   check_stub_type (arg_type);
733   type = TYPE_FIELD_TYPE (arg_type, fieldno);
734
735   /* Handle packed fields */
736
737   offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
738   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
739     {
740       v = value_from_longest (type,
741                            unpack_field_as_long (arg_type,
742                                                  VALUE_CONTENTS (arg1),
743                                                  fieldno));
744       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
745       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
746     }
747   else
748     {
749       v = allocate_value (type);
750       if (VALUE_LAZY (arg1))
751         VALUE_LAZY (v) = 1;
752       else
753         memcpy (VALUE_CONTENTS_RAW (v), VALUE_CONTENTS_RAW (arg1) + offset,
754                 TYPE_LENGTH (type));
755     }
756   VALUE_LVAL (v) = VALUE_LVAL (arg1);
757   if (VALUE_LVAL (arg1) == lval_internalvar)
758     VALUE_LVAL (v) = lval_internalvar_component;
759   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
760   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
761   return v;
762 }
763
764 /* Given a value ARG1 of a struct or union type,
765    extract and return the value of one of its fields.
766    FIELDNO says which field.
767
768    For C++, must also be able to return values from static fields */
769
770 value_ptr
771 value_field (arg1, fieldno)
772      register value_ptr arg1;
773      register int fieldno;
774 {
775   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
776 }
777
778 /* Return a non-virtual function as a value.
779    F is the list of member functions which contains the desired method.
780    J is an index into F which provides the desired method. */
781
782 value_ptr
783 value_fn_field (arg1p, f, j, type, offset)
784      value_ptr *arg1p;
785      struct fn_field *f;
786      int j;
787      struct type *type;
788      int offset;
789 {
790   register value_ptr v;
791   register struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
792   struct symbol *sym;
793
794   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
795                        0, VAR_NAMESPACE, 0, NULL);
796   if (! sym) 
797         return NULL;
798 /*
799         error ("Internal error: could not find physical method named %s",
800                     TYPE_FN_FIELD_PHYSNAME (f, j));
801 */
802   
803   v = allocate_value (ftype);
804   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
805   VALUE_TYPE (v) = ftype;
806
807   if (arg1p)
808    {
809     if (type != VALUE_TYPE (*arg1p))
810       *arg1p = value_ind (value_cast (lookup_pointer_type (type),
811                                       value_addr (*arg1p)));
812
813     /* Move the `this' pointer according to the offset. 
814     VALUE_OFFSET (*arg1p) += offset;
815     */
816     }
817
818   return v;
819 }
820
821 /* Return a virtual function as a value.
822    ARG1 is the object which provides the virtual function
823    table pointer.  *ARG1P is side-effected in calling this function.
824    F is the list of member functions which contains the desired virtual
825    function.
826    J is an index into F which provides the desired virtual function.
827
828    TYPE is the type in which F is located.  */
829 value_ptr
830 value_virtual_fn_field (arg1p, f, j, type, offset)
831      value_ptr *arg1p;
832      struct fn_field *f;
833      int j;
834      struct type *type;
835      int offset;
836 {
837   value_ptr arg1 = *arg1p;
838   /* First, get the virtual function table pointer.  That comes
839      with a strange type, so cast it to type `pointer to long' (which
840      should serve just fine as a function type).  Then, index into
841      the table, and convert final value to appropriate function type.  */
842   value_ptr entry, vfn, vtbl;
843   value_ptr vi = value_from_longest (builtin_type_int, 
844                                      (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
845   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
846   struct type *context;
847   if (fcontext == NULL)
848    /* We don't have an fcontext (e.g. the program was compiled with
849       g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
850       This won't work right for multiple inheritance, but at least we
851       should do as well as GDB 3.x did.  */
852     fcontext = TYPE_VPTR_BASETYPE (type);
853   context = lookup_pointer_type (fcontext);
854   /* Now context is a pointer to the basetype containing the vtbl.  */
855   if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
856     arg1 = value_ind (value_cast (context, value_addr (arg1)));
857
858   context = VALUE_TYPE (arg1);
859   /* Now context is the basetype containing the vtbl.  */
860
861   /* This type may have been defined before its virtual function table
862      was.  If so, fill in the virtual function table entry for the
863      type now.  */
864   if (TYPE_VPTR_FIELDNO (context) < 0)
865     fill_in_vptr_fieldno (context);
866
867   /* The virtual function table is now an array of structures
868      which have the form { int16 offset, delta; void *pfn; }.  */
869   vtbl = value_ind (value_primitive_field (arg1, 0, 
870                                            TYPE_VPTR_FIELDNO (context),
871                                            TYPE_VPTR_BASETYPE (context)));
872
873   /* Index into the virtual function table.  This is hard-coded because
874      looking up a field is not cheap, and it may be important to save
875      time, e.g. if the user has set a conditional breakpoint calling
876      a virtual function.  */
877   entry = value_subscript (vtbl, vi);
878
879   if (TYPE_CODE (VALUE_TYPE (entry)) == TYPE_CODE_STRUCT)
880     {
881       /* Move the `this' pointer according to the virtual function table. */
882       VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
883       
884       if (! VALUE_LAZY (arg1))
885         {
886           VALUE_LAZY (arg1) = 1;
887           value_fetch_lazy (arg1);
888         }
889
890       vfn = value_field (entry, 2);
891     }
892   else if (TYPE_CODE (VALUE_TYPE (entry)) == TYPE_CODE_PTR)
893     vfn = entry;
894   else
895     error ("I'm confused:  virtual function table has bad type");
896   /* Reinstantiate the function pointer with the correct type.  */
897   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
898
899   *arg1p = arg1;
900   return vfn;
901 }
902
903 /* ARG is a pointer to an object we know to be at least
904    a DTYPE.  BTYPE is the most derived basetype that has
905    already been searched (and need not be searched again).
906    After looking at the vtables between BTYPE and DTYPE,
907    return the most derived type we find.  The caller must
908    be satisfied when the return value == DTYPE.
909
910    FIXME-tiemann: should work with dossier entries as well.  */
911
912 static value_ptr
913 value_headof (in_arg, btype, dtype)
914      value_ptr in_arg;
915      struct type *btype, *dtype;
916 {
917   /* First collect the vtables we must look at for this object.  */
918   /* FIXME-tiemann: right now, just look at top-most vtable.  */
919   value_ptr arg, vtbl, entry, best_entry = 0;
920   int i, nelems;
921   int offset, best_offset = 0;
922   struct symbol *sym;
923   CORE_ADDR pc_for_sym;
924   char *demangled_name;
925   struct minimal_symbol *msymbol;
926
927   btype = TYPE_VPTR_BASETYPE (dtype);
928   check_stub_type (btype);
929   arg = in_arg;
930   if (btype != dtype)
931     arg = value_cast (lookup_pointer_type (btype), arg);
932   vtbl = value_ind (value_field (value_ind (arg), TYPE_VPTR_FIELDNO (btype)));
933
934   /* Check that VTBL looks like it points to a virtual function table.  */
935   msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
936   if (msymbol == NULL
937       || (demangled_name = SYMBOL_NAME (msymbol)) == NULL
938       || !VTBL_PREFIX_P (demangled_name))
939     {
940       /* If we expected to find a vtable, but did not, let the user
941          know that we aren't happy, but don't throw an error.
942          FIXME: there has to be a better way to do this.  */
943       struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
944       memcpy (error_type, VALUE_TYPE (in_arg), sizeof (struct type));
945       TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
946       VALUE_TYPE (in_arg) = error_type;
947       return in_arg;
948     }
949
950   /* Now search through the virtual function table.  */
951   entry = value_ind (vtbl);
952   nelems = longest_to_int (value_as_long (value_field (entry, 2)));
953   for (i = 1; i <= nelems; i++)
954     {
955       entry = value_subscript (vtbl, value_from_longest (builtin_type_int, 
956                                                       (LONGEST) i));
957       /* This won't work if we're using thunks. */
958       if (TYPE_CODE (VALUE_TYPE (entry)) != TYPE_CODE_STRUCT)
959         break;
960       offset = longest_to_int (value_as_long (value_field (entry, 0)));
961       /* If we use '<=' we can handle single inheritance
962        * where all offsets are zero - just use the first entry found. */
963       if (offset <= best_offset)
964         {
965           best_offset = offset;
966           best_entry = entry;
967         }
968     }
969   /* Move the pointer according to BEST_ENTRY's offset, and figure
970      out what type we should return as the new pointer.  */
971   if (best_entry == 0)
972     {
973       /* An alternative method (which should no longer be necessary).
974        * But we leave it in for future use, when we will hopefully
975        * have optimizes the vtable to use thunks instead of offsets. */
976       /* Use the name of vtable itself to extract a base type. */
977       demangled_name += 4;  /* Skip _vt$ prefix. */
978     }
979   else
980     {
981       pc_for_sym = value_as_pointer (value_field (best_entry, 2));
982       sym = find_pc_function (pc_for_sym);
983       demangled_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ANSI);
984       *(strchr (demangled_name, ':')) = '\0';
985     }
986   sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
987   if (sym == NULL)
988     error ("could not find type declaration for `%s'", demangled_name);
989   if (best_entry)
990     {
991       free (demangled_name);
992       arg = value_add (value_cast (builtin_type_int, arg),
993                        value_field (best_entry, 0));
994     }
995   else arg = in_arg;
996   VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
997   return arg;
998 }
999
1000 /* ARG is a pointer object of type TYPE.  If TYPE has virtual
1001    function tables, probe ARG's tables (including the vtables
1002    of its baseclasses) to figure out the most derived type that ARG
1003    could actually be a pointer to.  */
1004
1005 value_ptr
1006 value_from_vtable_info (arg, type)
1007      value_ptr arg;
1008      struct type *type;
1009 {
1010   /* Take care of preliminaries.  */
1011   if (TYPE_VPTR_FIELDNO (type) < 0)
1012     fill_in_vptr_fieldno (type);
1013   if (TYPE_VPTR_FIELDNO (type) < 0)
1014     return 0;
1015
1016   return value_headof (arg, 0, type);
1017 }
1018
1019 /* Return true if the INDEXth field of TYPE is a virtual baseclass
1020    pointer which is for the base class whose type is BASECLASS.  */
1021
1022 static int
1023 vb_match (type, index, basetype)
1024      struct type *type;
1025      int index;
1026      struct type *basetype;
1027 {
1028   struct type *fieldtype;
1029   char *name = TYPE_FIELD_NAME (type, index);
1030   char *field_class_name = NULL;
1031
1032   if (*name != '_')
1033     return 0;
1034   /* gcc 2.4 uses _vb$.  */
1035   if (name[1] == 'v' && name[2] == 'b' && name[3] == CPLUS_MARKER)
1036     field_class_name = name + 4;
1037   /* gcc 2.5 will use __vb_.  */
1038   if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_')
1039     field_class_name = name + 5;
1040
1041   if (field_class_name == NULL)
1042     /* This field is not a virtual base class pointer.  */
1043     return 0;
1044
1045   /* It's a virtual baseclass pointer, now we just need to find out whether
1046      it is for this baseclass.  */
1047   fieldtype = TYPE_FIELD_TYPE (type, index);
1048   if (fieldtype == NULL
1049       || TYPE_CODE (fieldtype) != TYPE_CODE_PTR)
1050     /* "Can't happen".  */
1051     return 0;
1052
1053   /* What we check for is that either the types are equal (needed for
1054      nameless types) or have the same name.  This is ugly, and a more
1055      elegant solution should be devised (which would probably just push
1056      the ugliness into symbol reading unless we change the stabs format).  */
1057   if (TYPE_TARGET_TYPE (fieldtype) == basetype)
1058     return 1;
1059
1060   if (TYPE_NAME (basetype) != NULL
1061       && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL
1062       && STREQ (TYPE_NAME (basetype),
1063                 TYPE_NAME (TYPE_TARGET_TYPE (fieldtype))))
1064     return 1;
1065   return 0;
1066 }
1067
1068 /* Compute the offset of the baseclass which is
1069    the INDEXth baseclass of class TYPE, for a value ARG,
1070    wih extra offset of OFFSET.
1071    The result is the offste of the baseclass value relative
1072    to (the address of)(ARG) + OFFSET.
1073
1074    -1 is returned on error. */
1075
1076 int
1077 baseclass_offset (type, index, arg, offset)
1078      struct type *type;
1079      int index;
1080      value_ptr arg;
1081      int offset;
1082 {
1083   struct type *basetype = TYPE_BASECLASS (type, index);
1084
1085   if (BASETYPE_VIA_VIRTUAL (type, index))
1086     {
1087       /* Must hunt for the pointer to this virtual baseclass.  */
1088       register int i, len = TYPE_NFIELDS (type);
1089       register int n_baseclasses = TYPE_N_BASECLASSES (type);
1090
1091       /* First look for the virtual baseclass pointer
1092          in the fields.  */
1093       for (i = n_baseclasses; i < len; i++)
1094         {
1095           if (vb_match (type, i, basetype))
1096             {
1097               CORE_ADDR addr
1098                 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1099                                   VALUE_CONTENTS (arg) + VALUE_OFFSET (arg)
1100                                   + offset
1101                                   + (TYPE_FIELD_BITPOS (type, i) / 8));
1102
1103               if (VALUE_LVAL (arg) != lval_memory)
1104                   return -1;
1105
1106               return addr -
1107                   (LONGEST) (VALUE_ADDRESS (arg) + VALUE_OFFSET (arg) + offset);
1108             }
1109         }
1110       /* Not in the fields, so try looking through the baseclasses.  */
1111       for (i = index+1; i < n_baseclasses; i++)
1112         {
1113           int boffset =
1114               baseclass_offset (type, i, arg, offset);
1115           if (boffset)
1116             return boffset;
1117         }
1118       /* Not found.  */
1119       return -1;
1120     }
1121
1122   /* Baseclass is easily computed.  */
1123   return TYPE_BASECLASS_BITPOS (type, index) / 8;
1124 }
1125
1126 /* Compute the address of the baseclass which is
1127    the INDEXth baseclass of class TYPE.  The TYPE base
1128    of the object is at VALADDR.
1129
1130    If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1131    or 0 if no error.  In that case the return value is not the address
1132    of the baseclasss, but the address which could not be read
1133    successfully.  */
1134
1135 /* FIXME Fix remaining uses of baseclass_addr to use baseclass_offset */
1136
1137 char *
1138 baseclass_addr (type, index, valaddr, valuep, errp)
1139      struct type *type;
1140      int index;
1141      char *valaddr;
1142      value_ptr *valuep;
1143      int *errp;
1144 {
1145   struct type *basetype = TYPE_BASECLASS (type, index);
1146
1147   if (errp)
1148     *errp = 0;
1149
1150   if (BASETYPE_VIA_VIRTUAL (type, index))
1151     {
1152       /* Must hunt for the pointer to this virtual baseclass.  */
1153       register int i, len = TYPE_NFIELDS (type);
1154       register int n_baseclasses = TYPE_N_BASECLASSES (type);
1155
1156       /* First look for the virtual baseclass pointer
1157          in the fields.  */
1158       for (i = n_baseclasses; i < len; i++)
1159         {
1160           if (vb_match (type, i, basetype))
1161             {
1162               value_ptr val = allocate_value (basetype);
1163               CORE_ADDR addr;
1164               int status;
1165
1166               addr
1167                 = unpack_pointer (TYPE_FIELD_TYPE (type, i),
1168                                   valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
1169
1170               status = target_read_memory (addr,
1171                                            VALUE_CONTENTS_RAW (val),
1172                                            TYPE_LENGTH (basetype));
1173               VALUE_LVAL (val) = lval_memory;
1174               VALUE_ADDRESS (val) = addr;
1175
1176               if (status != 0)
1177                 {
1178                   if (valuep)
1179                     *valuep = NULL;
1180                   release_value (val);
1181                   value_free (val);
1182                   if (errp)
1183                     *errp = status;
1184                   return (char *)addr;
1185                 }
1186               else
1187                 {
1188                   if (valuep)
1189                     *valuep = val;
1190                   return (char *) VALUE_CONTENTS (val);
1191                 }
1192             }
1193         }
1194       /* Not in the fields, so try looking through the baseclasses.  */
1195       for (i = index+1; i < n_baseclasses; i++)
1196         {
1197           char *baddr;
1198
1199           baddr = baseclass_addr (type, i, valaddr, valuep, errp);
1200           if (baddr)
1201             return baddr;
1202         }
1203       /* Not found.  */
1204       if (valuep)
1205         *valuep = 0;
1206       return 0;
1207     }
1208
1209   /* Baseclass is easily computed.  */
1210   if (valuep)
1211     *valuep = 0;
1212   return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1213 }
1214 \f
1215 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1216    VALADDR.
1217
1218    Extracting bits depends on endianness of the machine.  Compute the
1219    number of least significant bits to discard.  For big endian machines,
1220    we compute the total number of bits in the anonymous object, subtract
1221    off the bit count from the MSB of the object to the MSB of the
1222    bitfield, then the size of the bitfield, which leaves the LSB discard
1223    count.  For little endian machines, the discard count is simply the
1224    number of bits from the LSB of the anonymous object to the LSB of the
1225    bitfield.
1226
1227    If the field is signed, we also do sign extension. */
1228
1229 LONGEST
1230 unpack_field_as_long (type, valaddr, fieldno)
1231      struct type *type;
1232      char *valaddr;
1233      int fieldno;
1234 {
1235   unsigned LONGEST val;
1236   unsigned LONGEST valmask;
1237   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1238   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1239   int lsbcount;
1240
1241   val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1242
1243   /* Extract bits.  See comment above. */
1244
1245   if (BITS_BIG_ENDIAN)
1246     lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1247   else
1248     lsbcount = (bitpos % 8);
1249   val >>= lsbcount;
1250
1251   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1252      If the field is signed, and is negative, then sign extend. */
1253
1254   if ((bitsize > 0) && (bitsize < 8 * sizeof (val)))
1255     {
1256       valmask = (((unsigned LONGEST) 1) << bitsize) - 1;
1257       val &= valmask;
1258       if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type, fieldno)))
1259         {
1260           if (val & (valmask ^ (valmask >> 1)))
1261             {
1262               val |= ~valmask;
1263             }
1264         }
1265     }
1266   return (val);
1267 }
1268
1269 /* Modify the value of a bitfield.  ADDR points to a block of memory in
1270    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
1271    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
1272    indicate which bits (in target bit order) comprise the bitfield.  */
1273
1274 void
1275 modify_field (addr, fieldval, bitpos, bitsize)
1276      char *addr;
1277      LONGEST fieldval;
1278      int bitpos, bitsize;
1279 {
1280   LONGEST oword;
1281
1282   /* If a negative fieldval fits in the field in question, chop
1283      off the sign extension bits.  */
1284   if (bitsize < (8 * sizeof (fieldval))
1285       && (~fieldval & ~((1 << (bitsize - 1)) - 1)) == 0)
1286     fieldval = fieldval & ((1 << bitsize) - 1);
1287
1288   /* Warn if value is too big to fit in the field in question.  */
1289   if (bitsize < (8 * sizeof (fieldval))
1290       && 0 != (fieldval & ~((1<<bitsize)-1)))
1291     {
1292       /* FIXME: would like to include fieldval in the message, but
1293          we don't have a sprintf_longest.  */
1294       warning ("Value does not fit in %d bits.", bitsize);
1295
1296       /* Truncate it, otherwise adjoining fields may be corrupted.  */
1297       fieldval = fieldval & ((1 << bitsize) - 1);
1298     }
1299
1300   oword = extract_signed_integer (addr, sizeof oword);
1301
1302   /* Shifting for bit field depends on endianness of the target machine.  */
1303   if (BITS_BIG_ENDIAN)
1304     bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1305
1306   /* Mask out old value, while avoiding shifts >= size of oword */
1307   if (bitsize < 8 * sizeof (oword))
1308     oword &= ~(((((unsigned LONGEST)1) << bitsize) - 1) << bitpos);
1309   else
1310     oword &= ~((~(unsigned LONGEST)0) << bitpos);
1311   oword |= fieldval << bitpos;
1312
1313   store_signed_integer (addr, sizeof oword, oword);
1314 }
1315 \f
1316 /* Convert C numbers into newly allocated values */
1317
1318 value_ptr
1319 value_from_longest (type, num)
1320      struct type *type;
1321      register LONGEST num;
1322 {
1323   register value_ptr val = allocate_value (type);
1324   register enum type_code code = TYPE_CODE (type);
1325   register int len = TYPE_LENGTH (type);
1326
1327   switch (code)
1328     {
1329     case TYPE_CODE_INT:
1330     case TYPE_CODE_CHAR:
1331     case TYPE_CODE_ENUM:
1332     case TYPE_CODE_BOOL:
1333     case TYPE_CODE_RANGE:
1334       store_signed_integer (VALUE_CONTENTS_RAW (val), len, num);
1335       break;
1336       
1337     case TYPE_CODE_REF:
1338     case TYPE_CODE_PTR:
1339       /* This assumes that all pointers of a given length
1340          have the same form.  */
1341       store_address (VALUE_CONTENTS_RAW (val), len, (CORE_ADDR) num);
1342       break;
1343
1344     default:
1345       error ("Unexpected type encountered for integer constant.");
1346     }
1347   return val;
1348 }
1349
1350 value_ptr
1351 value_from_double (type, num)
1352      struct type *type;
1353      double num;
1354 {
1355   register value_ptr val = allocate_value (type);
1356   register enum type_code code = TYPE_CODE (type);
1357   register int len = TYPE_LENGTH (type);
1358
1359   if (code == TYPE_CODE_FLT)
1360     {
1361       store_floating (VALUE_CONTENTS_RAW (val), len, num);
1362     }
1363   else
1364     error ("Unexpected type encountered for floating constant.");
1365
1366   return val;
1367 }
1368 \f
1369 /* Deal with the value that is "about to be returned".  */
1370
1371 /* Return the value that a function returning now
1372    would be returning to its caller, assuming its type is VALTYPE.
1373    RETBUF is where we look for what ought to be the contents
1374    of the registers (in raw form).  This is because it is often
1375    desirable to restore old values to those registers
1376    after saving the contents of interest, and then call
1377    this function using the saved values.
1378    struct_return is non-zero when the function in question is
1379    using the structure return conventions on the machine in question;
1380    0 when it is using the value returning conventions (this often
1381    means returning pointer to where structure is vs. returning value). */
1382
1383 value_ptr
1384 value_being_returned (valtype, retbuf, struct_return)
1385      register struct type *valtype;
1386      char retbuf[REGISTER_BYTES];
1387      int struct_return;
1388      /*ARGSUSED*/
1389 {
1390   register value_ptr val;
1391   CORE_ADDR addr;
1392
1393 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1394   /* If this is not defined, just use EXTRACT_RETURN_VALUE instead.  */
1395   if (struct_return) {
1396     addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1397     if (!addr)
1398       error ("Function return value unknown");
1399     return value_at (valtype, addr);
1400   }
1401 #endif
1402
1403   val = allocate_value (valtype);
1404   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1405
1406   return val;
1407 }
1408
1409 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1410    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc
1411    and TYPE is the type (which is known to be struct, union or array).
1412
1413    On most machines, the struct convention is used unless we are
1414    using gcc and the type is of a special size.  */
1415 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1416    native compiler.  GCC 2.3.3 was the last release that did it the
1417    old way.  Since gcc2_compiled was not changed, we have no
1418    way to correctly win in all cases, so we just do the right thing
1419    for gcc1 and for gcc2 after this change.  Thus it loses for gcc
1420    2.0-2.3.3.  This is somewhat unfortunate, but changing gcc2_compiled
1421    would cause more chaos than dealing with some struct returns being
1422    handled wrong.  */
1423 #if !defined (USE_STRUCT_CONVENTION)
1424 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1425   (!((gcc_p == 1) && (TYPE_LENGTH (value_type) == 1                \
1426                       || TYPE_LENGTH (value_type) == 2             \
1427                       || TYPE_LENGTH (value_type) == 4             \
1428                       || TYPE_LENGTH (value_type) == 8             \
1429                       )                                            \
1430      ))
1431 #endif
1432
1433 /* Return true if the function specified is using the structure returning
1434    convention on this machine to return arguments, or 0 if it is using
1435    the value returning convention.  FUNCTION is the value representing
1436    the function, FUNCADDR is the address of the function, and VALUE_TYPE
1437    is the type returned by the function.  GCC_P is nonzero if compiled
1438    with GCC.  */
1439
1440 int
1441 using_struct_return (function, funcaddr, value_type, gcc_p)
1442      value_ptr function;
1443      CORE_ADDR funcaddr;
1444      struct type *value_type;
1445      int gcc_p;
1446      /*ARGSUSED*/
1447 {
1448   register enum type_code code = TYPE_CODE (value_type);
1449
1450   if (code == TYPE_CODE_ERROR)
1451     error ("Function return type unknown.");
1452
1453   if (code == TYPE_CODE_STRUCT ||
1454       code == TYPE_CODE_UNION ||
1455       code == TYPE_CODE_ARRAY)
1456     return USE_STRUCT_CONVENTION (gcc_p, value_type);
1457
1458   return 0;
1459 }
1460
1461 /* Store VAL so it will be returned if a function returns now.
1462    Does not verify that VAL's type matches what the current
1463    function wants to return.  */
1464
1465 void
1466 set_return_value (val)
1467      value_ptr val;
1468 {
1469   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1470
1471   if (code == TYPE_CODE_ERROR)
1472     error ("Function return type unknown.");
1473
1474   if (   code == TYPE_CODE_STRUCT
1475       || code == TYPE_CODE_UNION)       /* FIXME, implement struct return.  */
1476     error ("GDB does not support specifying a struct or union return value.");
1477
1478   STORE_RETURN_VALUE (VALUE_TYPE (val), VALUE_CONTENTS (val));
1479 }
1480 \f
1481 void
1482 _initialize_values ()
1483 {
1484   add_cmd ("convenience", no_class, show_convenience,
1485             "Debugger convenience (\"$foo\") variables.\n\
1486 These variables are created when you assign them values;\n\
1487 thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
1488 A few convenience variables are given values automatically:\n\
1489 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1490 \"$__\" holds the contents of the last address examined with \"x\".",
1491            &showlist);
1492
1493   add_cmd ("values", no_class, show_values,
1494            "Elements of value history around item number IDX (or last ten).",
1495            &showlist);
1496 }