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