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