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