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