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