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