gdb-3.3
[binutils-gdb.git] / gdb / values.c
1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "value.h"
25
26 /* The value-history records all the values printed
27 by print commands during this session. Each chunk
28 records 60 consecutive values. The first chunk on
29 the chain records the most recent values.
30 The total number of values is in value_history_count. */
31
32 #define VALUE_HISTORY_CHUNK 60
33
34 struct value_history_chunk
35 {
36 struct value_history_chunk *next;
37 value values[VALUE_HISTORY_CHUNK];
38 };
39
40 /* Chain of chunks now in use. */
41
42 static struct value_history_chunk *value_history_chain;
43
44 static int value_history_count; /* Abs number of last entry stored */
45
46 \f
47 /* List of all value objects currently allocated
48 (except for those released by calls to release_value)
49 This is so they can be freed after each command. */
50
51 static value all_values;
52
53 /* Allocate a value that has the correct length for type TYPE. */
54
55 value
56 allocate_value (type)
57 struct type *type;
58 {
59 register value val;
60
61 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
62 VALUE_NEXT (val) = all_values;
63 all_values = val;
64 VALUE_TYPE (val) = type;
65 VALUE_LVAL (val) = not_lval;
66 VALUE_ADDRESS (val) = 0;
67 VALUE_FRAME (val) = 0;
68 VALUE_OFFSET (val) = 0;
69 VALUE_BITPOS (val) = 0;
70 VALUE_BITSIZE (val) = 0;
71 VALUE_REPEATED (val) = 0;
72 VALUE_REPETITIONS (val) = 0;
73 VALUE_REGNO (val) = -1;
74 return val;
75 }
76
77 /* Allocate a value that has the correct length
78 for COUNT repetitions type TYPE. */
79
80 value
81 allocate_repeat_value (type, count)
82 struct type *type;
83 int count;
84 {
85 register value val;
86
87 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
88 VALUE_NEXT (val) = all_values;
89 all_values = val;
90 VALUE_TYPE (val) = type;
91 VALUE_LVAL (val) = not_lval;
92 VALUE_ADDRESS (val) = 0;
93 VALUE_FRAME (val) = 0;
94 VALUE_OFFSET (val) = 0;
95 VALUE_BITPOS (val) = 0;
96 VALUE_BITSIZE (val) = 0;
97 VALUE_REPEATED (val) = 1;
98 VALUE_REPETITIONS (val) = count;
99 VALUE_REGNO (val) = -1;
100 return val;
101 }
102
103 /* Free all the values that have been allocated (except for those released).
104 Called after each command, successful or not. */
105
106 void
107 free_all_values ()
108 {
109 register value val, next;
110
111 for (val = all_values; val; val = next)
112 {
113 next = VALUE_NEXT (val);
114 free (val);
115 }
116
117 all_values = 0;
118 }
119
120 /* Remove VAL from the chain all_values
121 so it will not be freed automatically. */
122
123 void
124 release_value (val)
125 register value val;
126 {
127 register value v;
128
129 if (all_values == val)
130 {
131 all_values = val->next;
132 return;
133 }
134
135 for (v = all_values; v; v = v->next)
136 {
137 if (v->next == val)
138 {
139 v->next = val->next;
140 break;
141 }
142 }
143 }
144
145 /* Return a copy of the value ARG.
146 It contains the same contents, for same memory address,
147 but it's a different block of storage. */
148
149 static value
150 value_copy (arg)
151 value arg;
152 {
153 register value val;
154 register struct type *type = VALUE_TYPE (arg);
155 if (VALUE_REPEATED (arg))
156 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
157 else
158 val = allocate_value (type);
159 VALUE_LVAL (val) = VALUE_LVAL (arg);
160 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
161 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
162 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
163 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
164 VALUE_REGNO (val) = VALUE_REGNO (arg);
165 bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val),
166 TYPE_LENGTH (VALUE_TYPE (arg))
167 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
168 return val;
169 }
170 \f
171 /* Access to the value history. */
172
173 /* Record a new value in the value history.
174 Returns the absolute history index of the entry. */
175
176 int
177 record_latest_value (val)
178 value val;
179 {
180 int i;
181 double foo;
182
183 /* Check error now if about to store an invalid float. We return -1
184 to the caller, but allow them to continue, e.g. to print it as "Nan". */
185 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
186 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
187 if (i) return -1; /* Indicate value not saved in history */
188 }
189
190 /* Here we treat value_history_count as origin-zero
191 and applying to the value being stored now. */
192
193 i = value_history_count % VALUE_HISTORY_CHUNK;
194 if (i == 0)
195 {
196 register struct value_history_chunk *new
197 = (struct value_history_chunk *)
198 xmalloc (sizeof (struct value_history_chunk));
199 bzero (new->values, sizeof new->values);
200 new->next = value_history_chain;
201 value_history_chain = new;
202 }
203
204 value_history_chain->values[i] = val;
205 release_value (val);
206
207 /* Now we regard value_history_count as origin-one
208 and applying to the value just stored. */
209
210 return ++value_history_count;
211 }
212
213 /* Return a copy of the value in the history with sequence number NUM. */
214
215 value
216 access_value_history (num)
217 int num;
218 {
219 register struct value_history_chunk *chunk;
220 register int i;
221 register int absnum = num;
222
223 if (absnum <= 0)
224 absnum += value_history_count;
225
226 if (absnum <= 0)
227 {
228 if (num == 0)
229 error ("The history is empty.");
230 else if (num == 1)
231 error ("There is only one value in the history.");
232 else
233 error ("History does not go back to $$%d.", -num);
234 }
235 if (absnum > value_history_count)
236 error ("History has not yet reached $%d.", absnum);
237
238 absnum--;
239
240 /* Now absnum is always absolute and origin zero. */
241
242 chunk = value_history_chain;
243 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
244 i > 0; i--)
245 chunk = chunk->next;
246
247 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
248 }
249
250 /* Clear the value history entirely.
251 Must be done when new symbol tables are loaded,
252 because the type pointers become invalid. */
253
254 void
255 clear_value_history ()
256 {
257 register struct value_history_chunk *next;
258 register int i;
259 register value val;
260
261 while (value_history_chain)
262 {
263 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
264 if (val = value_history_chain->values[i])
265 free (val);
266 next = value_history_chain->next;
267 free (value_history_chain);
268 value_history_chain = next;
269 }
270 value_history_count = 0;
271 }
272
273 static void
274 value_history_info (num_exp, from_tty)
275 char *num_exp;
276 int from_tty;
277 {
278 register int i;
279 register value val;
280 static int num = 1;
281
282 if (num_exp)
283 {
284 if (num_exp[0] == '+' && num_exp[1] == '\0')
285 /* "info history +" should print from the stored position. */
286 ;
287 else
288 /* "info history <exp>" should print around value number <exp>. */
289 num = parse_and_eval_address (num_exp) - 5;
290 }
291 else
292 {
293 /* "info history" means print the last 10 values. */
294 num = value_history_count - 9;
295 }
296
297 if (num <= 0)
298 num = 1;
299
300 for (i = num; i < num + 10 && i <= value_history_count; i++)
301 {
302 val = access_value_history (i);
303 printf_filtered ("$%d = ", i);
304 value_print (val, stdout, 0, Val_pretty_default);
305 printf_filtered ("\n");
306 }
307
308 /* The next "info history +" should start after what we just printed. */
309 num += 10;
310
311 /* Hitting just return after this command should do the same thing as
312 "info history +". If num_exp is null, this is unnecessary, since
313 "info history +" is not useful after "info history". */
314 if (from_tty && num_exp)
315 {
316 num_exp[0] = '+';
317 num_exp[1] = '\0';
318 }
319 }
320 \f
321 /* Internal variables. These are variables within the debugger
322 that hold values assigned by debugger commands.
323 The user refers to them with a '$' prefix
324 that does not appear in the variable names stored internally. */
325
326 static struct internalvar *internalvars;
327
328 /* Look up an internal variable with name NAME. NAME should not
329 normally include a dollar sign.
330
331 If the specified internal variable does not exist,
332 one is created, with a void value. */
333
334 struct internalvar *
335 lookup_internalvar (name)
336 char *name;
337 {
338 register struct internalvar *var;
339
340 for (var = internalvars; var; var = var->next)
341 if (!strcmp (var->name, name))
342 return var;
343
344 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
345 var->name = concat (name, "", "");
346 var->value = allocate_value (builtin_type_void);
347 release_value (var->value);
348 var->next = internalvars;
349 internalvars = var;
350 return var;
351 }
352
353 value
354 value_of_internalvar (var)
355 struct internalvar *var;
356 {
357 register value val;
358
359 #ifdef IS_TRAPPED_INTERNALVAR
360 if (IS_TRAPPED_INTERNALVAR (var->name))
361 return VALUE_OF_TRAPPED_INTERNALVAR (var);
362 #endif
363
364 val = value_copy (var->value);
365 VALUE_LVAL (val) = lval_internalvar;
366 VALUE_INTERNALVAR (val) = var;
367 return val;
368 }
369
370 void
371 set_internalvar_component (var, offset, bitpos, bitsize, newval)
372 struct internalvar *var;
373 int offset, bitpos, bitsize;
374 value newval;
375 {
376 register char *addr = VALUE_CONTENTS (var->value) + offset;
377
378 #ifdef IS_TRAPPED_INTERNALVAR
379 if (IS_TRAPPED_INTERNALVAR (var->name))
380 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
381 #endif
382
383 if (bitsize)
384 modify_field (addr, (int) value_as_long (newval),
385 bitpos, bitsize);
386 else
387 bcopy (VALUE_CONTENTS (newval), addr,
388 TYPE_LENGTH (VALUE_TYPE (newval)));
389 }
390
391 void
392 set_internalvar (var, val)
393 struct internalvar *var;
394 value val;
395 {
396 #ifdef IS_TRAPPED_INTERNALVAR
397 if (IS_TRAPPED_INTERNALVAR (var->name))
398 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
399 #endif
400
401 free (var->value);
402 var->value = value_copy (val);
403 release_value (var->value);
404 }
405
406 char *
407 internalvar_name (var)
408 struct internalvar *var;
409 {
410 return var->name;
411 }
412
413 /* Free all internalvars. Done when new symtabs are loaded,
414 because that makes the values invalid. */
415
416 void
417 clear_internalvars ()
418 {
419 register struct internalvar *var;
420
421 while (internalvars)
422 {
423 var = internalvars;
424 internalvars = var->next;
425 free (var->name);
426 free (var->value);
427 free (var);
428 }
429 }
430
431 static void
432 convenience_info ()
433 {
434 register struct internalvar *var;
435 int varseen = 0;
436
437 for (var = internalvars; var; var = var->next)
438 {
439 #ifdef IS_TRAPPED_INTERNALVAR
440 if (IS_TRAPPED_INTERNALVAR (var->name))
441 continue;
442 #endif
443 if (!varseen)
444 {
445 printf ("Debugger convenience variables:\n\n");
446 varseen = 1;
447 }
448 printf ("$%s: ", var->name);
449 value_print (var->value, stdout, 0, Val_pretty_default);
450 printf ("\n");
451 }
452 if (!varseen)
453 printf ("No debugger convenience variables now defined.\n\
454 Convenience variables have names starting with \"$\";\n\
455 use \"set\" as in \"set $foo = 5\" to define them.\n");
456 }
457 \f
458 /* Extract a value as a C number (either long or double).
459 Knows how to convert fixed values to double, or
460 floating values to long.
461 Does not deallocate the value. */
462
463 LONGEST
464 value_as_long (val)
465 register value val;
466 {
467 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
468 }
469
470 double
471 value_as_double (val)
472 register value val;
473 {
474 double foo;
475 int inv;
476
477 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
478 if (inv)
479 error ("Invalid floating value found in program.");
480 return foo;
481 }
482 \f
483 /* Unpack raw data (copied from debugee) at VALADDR
484 as a long, or as a double, assuming the raw data is described
485 by type TYPE. Knows how to convert different sizes of values
486 and can convert between fixed and floating point.
487
488 C++: It is assumed that the front-end has taken care of
489 all matters concerning pointers to members. A pointer
490 to member which reaches here is considered to be equivalent
491 to an INT (or some size). After all, it is only an offset. */
492
493 LONGEST
494 unpack_long (type, valaddr)
495 struct type *type;
496 char *valaddr;
497 {
498 register enum type_code code = TYPE_CODE (type);
499 register int len = TYPE_LENGTH (type);
500 register int nosign = TYPE_UNSIGNED (type);
501
502 if (code == TYPE_CODE_ENUM)
503 code = TYPE_CODE_INT;
504 if (code == TYPE_CODE_FLT)
505 {
506 if (len == sizeof (float))
507 return * (float *) valaddr;
508
509 if (len == sizeof (double))
510 return * (double *) valaddr;
511 }
512 else if (code == TYPE_CODE_INT && nosign)
513 {
514 if (len == sizeof (char))
515 return * (unsigned char *) valaddr;
516
517 if (len == sizeof (short))
518 return * (unsigned short *) valaddr;
519
520 if (len == sizeof (int))
521 return * (unsigned int *) valaddr;
522
523 if (len == sizeof (long))
524 return * (unsigned long *) valaddr;
525 #ifdef LONG_LONG
526 if (len == sizeof (long long))
527 return * (unsigned long long *) valaddr;
528 #endif
529 }
530 else if (code == TYPE_CODE_INT)
531 {
532 if (len == sizeof (char))
533 return * (char *) valaddr;
534
535 if (len == sizeof (short))
536 return * (short *) valaddr;
537
538 if (len == sizeof (int))
539 return * (int *) valaddr;
540
541 if (len == sizeof (long))
542 return * (long *) valaddr;
543
544 #ifdef LONG_LONG
545 if (len == sizeof (long long))
546 return * (long long *) valaddr;
547 #endif
548 }
549 else if (code == TYPE_CODE_PTR
550 || code == TYPE_CODE_REF)
551 {
552 if (len == sizeof (char *))
553 return (CORE_ADDR) * (char **) valaddr;
554 }
555 else if (code == TYPE_CODE_MEMBER)
556 error ("not implemented: member types in unpack_long");
557
558 error ("Value not integer or pointer.");
559 }
560
561 /* Return a double value from the specified type and address.
562 INVP points to an int which is set to 0 for valid value,
563 1 for invalid value (bad float format). In either case,
564 the returned double is OK to use. */
565
566 double
567 unpack_double (type, valaddr, invp)
568 struct type *type;
569 char *valaddr;
570 int *invp;
571 {
572 register enum type_code code = TYPE_CODE (type);
573 register int len = TYPE_LENGTH (type);
574 register int nosign = TYPE_UNSIGNED (type);
575
576 *invp = 0; /* Assume valid. */
577 if (code == TYPE_CODE_FLT)
578 {
579 if (INVALID_FLOAT (valaddr, len))
580 {
581 *invp = 1;
582 return 1.234567891011121314;
583 }
584
585 if (len == sizeof (float))
586 return * (float *) valaddr;
587
588 if (len == sizeof (double))
589 {
590 /* Some machines require doubleword alignment for doubles.
591 This code works on them, and on other machines. */
592 double temp;
593 bcopy ((char *) valaddr, (char *) &temp, sizeof (double));
594 return temp;
595 }
596 }
597 else if (code == TYPE_CODE_INT && nosign)
598 {
599 if (len == sizeof (char))
600 return * (unsigned char *) valaddr;
601
602 if (len == sizeof (short))
603 return * (unsigned short *) valaddr;
604
605 if (len == sizeof (int))
606 return * (unsigned int *) valaddr;
607
608 if (len == sizeof (long))
609 return * (unsigned long *) valaddr;
610
611 #ifdef LONG_LONG
612 if (len == sizeof (long long))
613 return * (unsigned long long *) valaddr;
614 #endif
615 }
616 else if (code == TYPE_CODE_INT)
617 {
618 if (len == sizeof (char))
619 return * (char *) valaddr;
620
621 if (len == sizeof (short))
622 return * (short *) valaddr;
623
624 if (len == sizeof (int))
625 return * (int *) valaddr;
626
627 if (len == sizeof (long))
628 return * (long *) valaddr;
629
630 #ifdef LONG_LONG
631 if (len == sizeof (long long))
632 return * (long long *) valaddr;
633 #endif
634 }
635
636 error ("Value not floating number.");
637 /* NOTREACHED */
638 return (double) 0; /* To silence compiler warning. */
639 }
640 \f
641 /* Given a value ARG1 of a struct or union type,
642 extract and return the value of one of its fields.
643 FIELDNO says which field.
644
645 For C++, must also be able to return values from static fields */
646
647 value
648 value_field (arg1, fieldno)
649 register value arg1;
650 register int fieldno;
651 {
652 register value v;
653 register struct type *type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno);
654 register int offset;
655
656 /* Handle packed fields */
657
658 offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8;
659 if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno))
660 {
661 v = value_from_long (type,
662 unpack_field_as_long (VALUE_TYPE (arg1),
663 VALUE_CONTENTS (arg1),
664 fieldno));
665 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8;
666 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno);
667 }
668 else
669 {
670 v = allocate_value (type);
671 bcopy (VALUE_CONTENTS (arg1) + offset,
672 VALUE_CONTENTS (v),
673 TYPE_LENGTH (type));
674 }
675 VALUE_LVAL (v) = VALUE_LVAL (arg1);
676 if (VALUE_LVAL (arg1) == lval_internalvar)
677 VALUE_LVAL (v) = lval_internalvar_component;
678 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
679 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
680 return v;
681 }
682
683 value
684 value_fn_field (arg1, fieldno, subfieldno)
685 register value arg1;
686 register int fieldno;
687 {
688 register value v;
689 struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
690 register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
691 struct symbol *sym;
692
693 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
694 0, VAR_NAMESPACE, 0);
695 if (! sym) error ("Internal error: could not find physical method named %s",
696 TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
697
698 v = allocate_value (type);
699 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
700 VALUE_TYPE (v) = type;
701 return v;
702 }
703
704 /* Return a virtual function as a value.
705 ARG1 is the object which provides the virtual function
706 table pointer.
707 F is the list of member functions which contains the desired virtual
708 function.
709 J is an index into F which provides the desired virtual function.
710 TYPE is the basetype which first provides the virtual function table. */
711 value
712 value_virtual_fn_field (arg1, f, j, type)
713 value arg1;
714 struct fn_field *f;
715 int j;
716 struct type *type;
717 {
718 /* First, get the virtual function table pointer. That comes
719 with a strange type, so cast it to type `pointer to long' (which
720 should serve just fine as a function type). Then, index into
721 the table, and convert final value to appropriate function type. */
722 value vfn, vtbl;
723 value vi = value_from_long (builtin_type_int,
724 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
725 VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type);
726
727 /* This type may have been defined before its virtual function table
728 was. If so, fill in the virtual function table entry for the
729 type now. */
730 if (TYPE_VPTR_FIELDNO (type) < 0)
731 TYPE_VPTR_FIELDNO (type)
732 = fill_in_vptr_fieldno (type);
733
734 /* The virtual function table is now an array of structures
735 which have the form { int16 offset, delta; void *pfn; }. */
736 vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type)));
737
738 /* Index into the virtual function table. This is hard-coded because
739 looking up a field is not cheap, and it may be important to save
740 time, e.g. if the user has set a conditional breakpoint calling
741 a virtual function. */
742 vfn = value_field (value_subscript (vtbl, vi), 2);
743
744 /* Reinstantiate the function pointer with the correct type. */
745 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
746 return vfn;
747 }
748
749 /* The value of a static class member does not depend
750 on its instance, only on its type. If FIELDNO >= 0,
751 then fieldno is a valid field number and is used directly.
752 Otherwise, FIELDNAME is the name of the field we are
753 searching for. If it is not a static field name, an
754 error is signaled. TYPE is the type in which we look for the
755 static field member. */
756 value
757 value_static_field (type, fieldname, fieldno)
758 register struct type *type;
759 char *fieldname;
760 register int fieldno;
761 {
762 register value v;
763 struct symbol *sym;
764
765 if (fieldno < 0)
766 {
767 register struct type *t = type;
768 /* Look for static field. */
769 while (t)
770 {
771 int i;
772 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
773 if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
774 {
775 if (TYPE_FIELD_STATIC (t, i))
776 {
777 fieldno = i;
778 goto found;
779 }
780 else
781 error ("field `%s' is not static");
782 }
783 t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
784 }
785
786 t = type;
787
788 if (destructor_name_p (fieldname, t))
789 error ("use `info method' command to print out value of destructor");
790
791 while (t)
792 {
793 int i, j;
794
795 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
796 {
797 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
798 {
799 error ("use `info method' command to print value of method \"%s\"", fieldname);
800 }
801 }
802 t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
803 }
804 error("there is no field named %s", fieldname);
805 }
806
807 found:
808
809 sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno),
810 0, VAR_NAMESPACE, 0);
811 if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno));
812
813 type = TYPE_FIELD_TYPE (type, fieldno);
814 v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
815 return v;
816 }
817
818 long
819 unpack_field_as_long (type, valaddr, fieldno)
820 struct type *type;
821 char *valaddr;
822 int fieldno;
823 {
824 long val;
825 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
826 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
827
828 bcopy (valaddr + bitpos / 8, &val, sizeof val);
829
830 /* Extracting bits depends on endianness of the machine. */
831 #ifdef BITS_BIG_ENDIAN
832 val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
833 #else
834 val = val >> (bitpos % 8);
835 #endif
836
837 val &= (1 << bitsize) - 1;
838 return val;
839 }
840
841 void
842 modify_field (addr, fieldval, bitpos, bitsize)
843 char *addr;
844 int fieldval;
845 int bitpos, bitsize;
846 {
847 long oword;
848
849 /* Reject values too big to fit in the field in question.
850 Otherwise adjoining fields may be corrupted. */
851 if (fieldval & ~((1<<bitsize)-1))
852 error ("Value %d does not fit in %d bits.", fieldval, bitsize);
853
854 bcopy (addr, &oword, sizeof oword);
855
856 /* Shifting for bit field depends on endianness of the machine. */
857 #ifdef BITS_BIG_ENDIAN
858 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
859 #endif
860
861 oword &= ~(((1 << bitsize) - 1) << bitpos);
862 oword |= fieldval << bitpos;
863 bcopy (&oword, addr, sizeof oword);
864 }
865 \f
866 /* Convert C numbers into newly allocated values */
867
868 value
869 value_from_long (type, num)
870 struct type *type;
871 register LONGEST num;
872 {
873 register value val = allocate_value (type);
874 register enum type_code code = TYPE_CODE (type);
875 register int len = TYPE_LENGTH (type);
876
877 if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
878 {
879 if (len == sizeof (char))
880 * (char *) VALUE_CONTENTS (val) = num;
881 else if (len == sizeof (short))
882 * (short *) VALUE_CONTENTS (val) = num;
883 else if (len == sizeof (int))
884 * (int *) VALUE_CONTENTS (val) = num;
885 else if (len == sizeof (long))
886 * (long *) VALUE_CONTENTS (val) = num;
887 #ifdef LONG_LONG
888 else if (len == sizeof (long long))
889 * (long long *) VALUE_CONTENTS (val) = num;
890 #endif
891 else
892 error ("Integer type encountered with unexpected data length.");
893 }
894 else
895 error ("Unexpected type encountered for integer constant.");
896
897 return val;
898 }
899
900 value
901 value_from_double (type, num)
902 struct type *type;
903 double num;
904 {
905 register value val = allocate_value (type);
906 register enum type_code code = TYPE_CODE (type);
907 register int len = TYPE_LENGTH (type);
908
909 if (code == TYPE_CODE_FLT)
910 {
911 if (len == sizeof (float))
912 * (float *) VALUE_CONTENTS (val) = num;
913 else if (len == sizeof (double))
914 * (double *) VALUE_CONTENTS (val) = num;
915 else
916 error ("Floating type encountered with unexpected data length.");
917 }
918 else
919 error ("Unexpected type encountered for floating constant.");
920
921 return val;
922 }
923 \f
924 /* Deal with the value that is "about to be returned". */
925
926 /* Return the value that a function returning now
927 would be returning to its caller, assuming its type is VALTYPE.
928 RETBUF is where we look for what ought to be the contents
929 of the registers (in raw form). This is because it is often
930 desirable to restore old values to those registers
931 after saving the contents of interest, and then call
932 this function using the saved values.
933 struct_return is non-zero when the function in question is
934 using the structure return conventions on the machine in question;
935 0 when it is using the value returning conventions (this often
936 means returning pointer to where structure is vs. returning value). */
937
938 value
939 value_being_returned (valtype, retbuf, struct_return)
940 register struct type *valtype;
941 char retbuf[REGISTER_BYTES];
942 int struct_return;
943 {
944 register value val;
945
946 if (struct_return)
947 return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf));
948
949 val = allocate_value (valtype);
950 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val));
951
952 return val;
953 }
954
955 /* Return true if the function specified is using the structure returning
956 convention on this machine to return arguments, or 0 if it is using
957 the value returning convention. FUNCTION is the value representing
958 the function, FUNCADDR is the address of the function, and VALUE_TYPE
959 is the type returned by the function */
960
961 struct block *block_for_pc ();
962
963 int
964 using_struct_return (function, funcaddr, value_type)
965 value function;
966 CORE_ADDR funcaddr;
967 struct type *value_type;
968 {
969 register enum type_code code = TYPE_CODE (value_type);
970
971 if (code == TYPE_CODE_STRUCT ||
972 code == TYPE_CODE_UNION ||
973 code == TYPE_CODE_ARRAY)
974 {
975 struct block *b = block_for_pc (funcaddr);
976
977 if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8))
978 return 1;
979 }
980
981 return 0;
982 }
983
984 /* Store VAL so it will be returned if a function returns now.
985 Does not verify that VAL's type matches what the current
986 function wants to return. */
987
988 void
989 set_return_value (val)
990 value val;
991 {
992 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
993 char regbuf[REGISTER_BYTES];
994 double dbuf;
995 LONGEST lbuf;
996
997 if (code == TYPE_CODE_STRUCT
998 || code == TYPE_CODE_UNION)
999 error ("Specifying a struct or union return value is not supported.");
1000
1001 if (code == TYPE_CODE_FLT)
1002 {
1003 dbuf = value_as_double (val);
1004
1005 STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf);
1006 }
1007 else
1008 {
1009 lbuf = value_as_long (val);
1010 STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf);
1011 }
1012 }
1013 \f
1014 void
1015 _initialize_values ()
1016 {
1017 add_info ("convenience", convenience_info,
1018 "Debugger convenience (\"$foo\") variables.\n\
1019 These variables are created when you assign them values;\n\
1020 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1021 A few convenience variables are given values automatically GDB:\n\
1022 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1023 \"$__\" holds the contents of the last address examined with \"x\".");
1024
1025 add_info ("values", value_history_info,
1026 "Elements of value history (around item number IDX, or last ten).");
1027 add_info_alias ("history", value_history_info, 0);
1028 }