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