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