2003-01-13 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
c906108c 1/* Find a variable's value in memory, for GDB, the GNU debugger.
1bac305b
AC
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003 Free Software
5 Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
c906108c
SS
23
24#include "defs.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "frame.h"
28#include "value.h"
29#include "gdbcore.h"
30#include "inferior.h"
31#include "target.h"
32#include "gdb_string.h"
14e534aa 33#include "gdb_assert.h"
c906108c 34#include "floatformat.h"
c5aa993b 35#include "symfile.h" /* for overlay functions */
4e052eda 36#include "regcache.h"
0406ec40 37#include "builtin-regs.h"
c906108c 38
c906108c
SS
39/* Basic byte-swapping routines. GDB has needed these for a long time...
40 All extract a target-format integer at ADDR which is LEN bytes long. */
41
42#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
43 /* 8 bit characters are a pretty safe assumption these days, so we
44 assume it throughout all these swapping routines. If we had to deal with
45 9 bit characters, we would need to make len be in bits and would have
46 to re-write these routines... */
c5aa993b 47you lose
c906108c
SS
48#endif
49
a9ac8f51 50LONGEST
37611a2b 51extract_signed_integer (const void *addr, int len)
c906108c
SS
52{
53 LONGEST retval;
37611a2b
AC
54 const unsigned char *p;
55 const unsigned char *startaddr = addr;
56 const unsigned char *endaddr = startaddr + len;
c906108c
SS
57
58 if (len > (int) sizeof (LONGEST))
59 error ("\
60That operation is not available on integers of more than %d bytes.",
baa6f10b 61 (int) sizeof (LONGEST));
c906108c
SS
62
63 /* Start at the most significant end of the integer, and work towards
64 the least significant. */
d7449b42 65 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
66 {
67 p = startaddr;
68 /* Do the sign extension once at the start. */
c5aa993b 69 retval = ((LONGEST) * p ^ 0x80) - 0x80;
c906108c
SS
70 for (++p; p < endaddr; ++p)
71 retval = (retval << 8) | *p;
72 }
73 else
74 {
75 p = endaddr - 1;
76 /* Do the sign extension once at the start. */
c5aa993b 77 retval = ((LONGEST) * p ^ 0x80) - 0x80;
c906108c
SS
78 for (--p; p >= startaddr; --p)
79 retval = (retval << 8) | *p;
80 }
81 return retval;
82}
83
84ULONGEST
37611a2b 85extract_unsigned_integer (const void *addr, int len)
c906108c
SS
86{
87 ULONGEST retval;
37611a2b
AC
88 const unsigned char *p;
89 const unsigned char *startaddr = addr;
90 const unsigned char *endaddr = startaddr + len;
c906108c
SS
91
92 if (len > (int) sizeof (ULONGEST))
93 error ("\
94That operation is not available on integers of more than %d bytes.",
baa6f10b 95 (int) sizeof (ULONGEST));
c906108c
SS
96
97 /* Start at the most significant end of the integer, and work towards
98 the least significant. */
99 retval = 0;
d7449b42 100 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
101 {
102 for (p = startaddr; p < endaddr; ++p)
103 retval = (retval << 8) | *p;
104 }
105 else
106 {
107 for (p = endaddr - 1; p >= startaddr; --p)
108 retval = (retval << 8) | *p;
109 }
110 return retval;
111}
112
113/* Sometimes a long long unsigned integer can be extracted as a
114 LONGEST value. This is done so that we can print these values
115 better. If this integer can be converted to a LONGEST, this
116 function returns 1 and sets *PVAL. Otherwise it returns 0. */
117
118int
66140c26 119extract_long_unsigned_integer (const void *addr, int orig_len, LONGEST *pval)
c906108c
SS
120{
121 char *p, *first_addr;
122 int len;
123
124 len = orig_len;
d7449b42 125 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
126 {
127 for (p = (char *) addr;
128 len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
129 p++)
130 {
131 if (*p == 0)
132 len--;
133 else
134 break;
135 }
136 first_addr = p;
137 }
138 else
139 {
140 first_addr = (char *) addr;
141 for (p = (char *) addr + orig_len - 1;
142 len > (int) sizeof (LONGEST) && p >= (char *) addr;
143 p--)
144 {
145 if (*p == 0)
146 len--;
147 else
148 break;
149 }
150 }
151
152 if (len <= (int) sizeof (LONGEST))
153 {
154 *pval = (LONGEST) extract_unsigned_integer (first_addr,
155 sizeof (LONGEST));
156 return 1;
157 }
158
159 return 0;
160}
161
4478b372
JB
162
163/* Treat the LEN bytes at ADDR as a target-format address, and return
164 that address. ADDR is a buffer in the GDB process, not in the
165 inferior.
166
167 This function should only be used by target-specific code. It
168 assumes that a pointer has the same representation as that thing's
169 address represented as an integer. Some machines use word
170 addresses, or similarly munged things, for certain types of
171 pointers, so that assumption doesn't hold everywhere.
172
173 Common code should use extract_typed_address instead, or something
174 else based on POINTER_TO_ADDRESS. */
175
c906108c 176CORE_ADDR
66140c26 177extract_address (const void *addr, int len)
c906108c
SS
178{
179 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
180 whether we want this to be true eventually. */
c5aa993b 181 return (CORE_ADDR) extract_unsigned_integer (addr, len);
c906108c
SS
182}
183
4478b372 184
4478b372
JB
185/* Treat the bytes at BUF as a pointer of type TYPE, and return the
186 address it represents. */
187CORE_ADDR
66140c26 188extract_typed_address (const void *buf, struct type *type)
4478b372
JB
189{
190 if (TYPE_CODE (type) != TYPE_CODE_PTR
191 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28
AC
192 internal_error (__FILE__, __LINE__,
193 "extract_typed_address: "
4478b372
JB
194 "type is not a pointer or reference");
195
196 return POINTER_TO_ADDRESS (type, buf);
197}
198
199
c906108c 200void
a9ac8f51 201store_signed_integer (void *addr, int len, LONGEST val)
c906108c
SS
202{
203 unsigned char *p;
c5aa993b 204 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
205 unsigned char *endaddr = startaddr + len;
206
207 /* Start at the least significant end of the integer, and work towards
208 the most significant. */
d7449b42 209 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
210 {
211 for (p = endaddr - 1; p >= startaddr; --p)
212 {
213 *p = val & 0xff;
214 val >>= 8;
215 }
216 }
217 else
218 {
219 for (p = startaddr; p < endaddr; ++p)
220 {
221 *p = val & 0xff;
222 val >>= 8;
223 }
224 }
225}
226
227void
a9ac8f51 228store_unsigned_integer (void *addr, int len, ULONGEST val)
c906108c
SS
229{
230 unsigned char *p;
c5aa993b 231 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
232 unsigned char *endaddr = startaddr + len;
233
234 /* Start at the least significant end of the integer, and work towards
235 the most significant. */
d7449b42 236 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
237 {
238 for (p = endaddr - 1; p >= startaddr; --p)
239 {
240 *p = val & 0xff;
241 val >>= 8;
242 }
243 }
244 else
245 {
246 for (p = startaddr; p < endaddr; ++p)
247 {
248 *p = val & 0xff;
249 val >>= 8;
250 }
251 }
252}
253
4478b372
JB
254/* Store the address VAL as a LEN-byte value in target byte order at
255 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
256
257 This function should only be used by target-specific code. It
258 assumes that a pointer has the same representation as that thing's
259 address represented as an integer. Some machines use word
260 addresses, or similarly munged things, for certain types of
261 pointers, so that assumption doesn't hold everywhere.
262
263 Common code should use store_typed_address instead, or something else
264 based on ADDRESS_TO_POINTER. */
c906108c 265void
a9ac8f51 266store_address (void *addr, int len, LONGEST val)
c906108c 267{
c906108c
SS
268 store_unsigned_integer (addr, len, val);
269}
4478b372
JB
270
271
4478b372
JB
272/* Store the address ADDR as a pointer of type TYPE at BUF, in target
273 form. */
274void
275store_typed_address (void *buf, struct type *type, CORE_ADDR addr)
276{
277 if (TYPE_CODE (type) != TYPE_CODE_PTR
278 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28
AC
279 internal_error (__FILE__, __LINE__,
280 "store_typed_address: "
4478b372
JB
281 "type is not a pointer or reference");
282
283 ADDRESS_TO_POINTER (type, buf, addr);
284}
285
286
287
376c9600
AC
288/* Return a `value' with the contents of (virtual or cooked) register
289 REGNUM as found in the specified FRAME. The register's type is
290 determined by REGISTER_VIRTUAL_TYPE.
c906108c 291
376c9600
AC
292 NOTE: returns NULL if register value is not available. Caller will
293 check return value or die! */
c906108c 294
3d6d86c6 295struct value *
376c9600 296value_of_register (int regnum, struct frame_info *frame)
c906108c
SS
297{
298 CORE_ADDR addr;
299 int optim;
3d6d86c6 300 struct value *reg_val;
e6cbd02a 301 char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
302 enum lval_type lval;
303
0406ec40
AC
304 /* Builtin registers lie completly outside of the range of normal
305 registers. Catch them early so that the target never sees them. */
306 if (regnum >= NUM_REGS + NUM_PSEUDO_REGS)
6e7f8b9c 307 return value_of_builtin_reg (regnum, deprecated_selected_frame);
0406ec40 308
c906108c 309 get_saved_register (raw_buffer, &optim, &addr,
376c9600 310 frame, regnum, &lval);
c906108c 311
c97dcfc7
AC
312 /* FIXME: cagney/2002-05-15: This test is just bogus.
313
314 It indicates that the target failed to supply a value for a
315 register because it was "not available" at this time. Problem
316 is, the target still has the register and so get saved_register()
317 may be returning a value saved on the stack. */
318
32178cab 319 if (register_cached (regnum) < 0)
c5aa993b 320 return NULL; /* register value not available */
c906108c
SS
321
322 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
323
324 /* Convert raw data to virtual format if necessary. */
325
c906108c
SS
326 if (REGISTER_CONVERTIBLE (regnum))
327 {
328 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
329 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
330 }
392a587b
JM
331 else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
332 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
333 REGISTER_RAW_SIZE (regnum));
c906108c 334 else
8e65ff28
AC
335 internal_error (__FILE__, __LINE__,
336 "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
96baa820
JM
337 REGISTER_NAME (regnum),
338 regnum,
339 REGISTER_RAW_SIZE (regnum),
340 REGISTER_VIRTUAL_SIZE (regnum));
c906108c
SS
341 VALUE_LVAL (reg_val) = lval;
342 VALUE_ADDRESS (reg_val) = addr;
343 VALUE_REGNO (reg_val) = regnum;
344 VALUE_OPTIMIZED_OUT (reg_val) = optim;
345 return reg_val;
346}
4478b372
JB
347
348/* Given a pointer of type TYPE in target form in BUF, return the
349 address it represents. */
350CORE_ADDR
66140c26 351unsigned_pointer_to_address (struct type *type, const void *buf)
4478b372
JB
352{
353 return extract_address (buf, TYPE_LENGTH (type));
354}
355
ac2e2ef7 356CORE_ADDR
66140c26 357signed_pointer_to_address (struct type *type, const void *buf)
ac2e2ef7
AC
358{
359 return extract_signed_integer (buf, TYPE_LENGTH (type));
360}
4478b372
JB
361
362/* Given an address, store it as a pointer of type TYPE in target
363 format in BUF. */
364void
ac2e2ef7 365unsigned_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
4478b372
JB
366{
367 store_address (buf, TYPE_LENGTH (type), addr);
368}
369
ac2e2ef7
AC
370void
371address_to_signed_pointer (struct type *type, void *buf, CORE_ADDR addr)
372{
373 store_signed_integer (buf, TYPE_LENGTH (type), addr);
374}
c906108c
SS
375\f
376/* Will calling read_var_value or locate_var_value on SYM end
377 up caring what frame it is being evaluated relative to? SYM must
378 be non-NULL. */
379int
fba45db2 380symbol_read_needs_frame (struct symbol *sym)
c906108c
SS
381{
382 switch (SYMBOL_CLASS (sym))
383 {
384 /* All cases listed explicitly so that gcc -Wall will detect it if
c5aa993b 385 we failed to consider one. */
c906108c
SS
386 case LOC_REGISTER:
387 case LOC_ARG:
388 case LOC_REF_ARG:
389 case LOC_REGPARM:
390 case LOC_REGPARM_ADDR:
391 case LOC_LOCAL:
392 case LOC_LOCAL_ARG:
393 case LOC_BASEREG:
394 case LOC_BASEREG_ARG:
407caf07 395 case LOC_HP_THREAD_LOCAL_STATIC:
c906108c
SS
396 return 1;
397
398 case LOC_UNDEF:
399 case LOC_CONST:
400 case LOC_STATIC:
401 case LOC_INDIRECT:
402 case LOC_TYPEDEF:
403
404 case LOC_LABEL:
405 /* Getting the address of a label can be done independently of the block,
c5aa993b
JM
406 even if some *uses* of that address wouldn't work so well without
407 the right frame. */
c906108c
SS
408
409 case LOC_BLOCK:
410 case LOC_CONST_BYTES:
411 case LOC_UNRESOLVED:
412 case LOC_OPTIMIZED_OUT:
413 return 0;
414 }
415 return 1;
416}
417
418/* Given a struct symbol for a variable,
419 and a stack frame id, read the value of the variable
420 and return a (pointer to a) struct value containing the value.
421 If the variable cannot be found, return a zero pointer.
6e7f8b9c 422 If FRAME is NULL, use the deprecated_selected_frame. */
c906108c 423
3d6d86c6 424struct value *
fba45db2 425read_var_value (register struct symbol *var, struct frame_info *frame)
c906108c 426{
3d6d86c6 427 register struct value *v;
c906108c
SS
428 struct type *type = SYMBOL_TYPE (var);
429 CORE_ADDR addr;
430 register int len;
431
432 v = allocate_value (type);
433 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
434 VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
435
436 len = TYPE_LENGTH (type);
437
c5aa993b 438 if (frame == NULL)
6e7f8b9c 439 frame = deprecated_selected_frame;
c906108c
SS
440
441 switch (SYMBOL_CLASS (var))
442 {
443 case LOC_CONST:
444 /* Put the constant back in target format. */
445 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
446 (LONGEST) SYMBOL_VALUE (var));
447 VALUE_LVAL (v) = not_lval;
448 return v;
449
450 case LOC_LABEL:
451 /* Put the constant back in target format. */
452 if (overlay_debugging)
4478b372
JB
453 {
454 CORE_ADDR addr
455 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
456 SYMBOL_BFD_SECTION (var));
457 store_typed_address (VALUE_CONTENTS_RAW (v), type, addr);
458 }
c906108c 459 else
4478b372
JB
460 store_typed_address (VALUE_CONTENTS_RAW (v), type,
461 SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
462 VALUE_LVAL (v) = not_lval;
463 return v;
464
465 case LOC_CONST_BYTES:
466 {
467 char *bytes_addr;
468 bytes_addr = SYMBOL_VALUE_BYTES (var);
469 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
470 VALUE_LVAL (v) = not_lval;
471 return v;
472 }
473
474 case LOC_STATIC:
475 if (overlay_debugging)
476 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
477 SYMBOL_BFD_SECTION (var));
478 else
479 addr = SYMBOL_VALUE_ADDRESS (var);
480 break;
481
482 case LOC_INDIRECT:
f76febae
AC
483 {
484 /* The import slot does not have a real address in it from the
485 dynamic loader (dld.sl on HP-UX), if the target hasn't
486 begun execution yet, so check for that. */
487 CORE_ADDR locaddr;
488 struct value *loc;
489 if (!target_has_execution)
490 error ("\
c906108c
SS
491Attempt to access variable defined in different shared object or load module when\n\
492addresses have not been bound by the dynamic loader. Try again when executable is running.");
c5aa993b 493
f76febae
AC
494 locaddr = SYMBOL_VALUE_ADDRESS (var);
495 loc = value_at (lookup_pointer_type (type), locaddr, NULL);
1aa20aa8 496 addr = value_as_address (loc);
f76febae 497 }
c906108c
SS
498
499 case LOC_ARG:
500 if (frame == NULL)
501 return 0;
502 addr = FRAME_ARGS_ADDRESS (frame);
503 if (!addr)
504 return 0;
505 addr += SYMBOL_VALUE (var);
506 break;
507
508 case LOC_REF_ARG:
f76febae
AC
509 {
510 struct value *ref;
511 CORE_ADDR argref;
512 if (frame == NULL)
513 return 0;
514 argref = FRAME_ARGS_ADDRESS (frame);
515 if (!argref)
516 return 0;
517 argref += SYMBOL_VALUE (var);
518 ref = value_at (lookup_pointer_type (type), argref, NULL);
1aa20aa8 519 addr = value_as_address (ref);
f76febae
AC
520 break;
521 }
c906108c
SS
522
523 case LOC_LOCAL:
524 case LOC_LOCAL_ARG:
525 if (frame == NULL)
526 return 0;
527 addr = FRAME_LOCALS_ADDRESS (frame);
528 addr += SYMBOL_VALUE (var);
529 break;
530
531 case LOC_BASEREG:
532 case LOC_BASEREG_ARG:
407caf07 533 case LOC_HP_THREAD_LOCAL_STATIC:
c906108c 534 {
3d6d86c6 535 struct value *regval;
c5aa993b 536
9ed10b08
ND
537 regval = value_from_register (lookup_pointer_type (type),
538 SYMBOL_BASEREG (var), frame);
539 if (regval == NULL)
540 error ("Value of base register not available.");
1aa20aa8 541 addr = value_as_address (regval);
c5aa993b
JM
542 addr += SYMBOL_VALUE (var);
543 break;
c906108c 544 }
c5aa993b 545
9d774e44
EZ
546 case LOC_THREAD_LOCAL_STATIC:
547 {
9d774e44
EZ
548 if (target_get_thread_local_address_p ())
549 addr = target_get_thread_local_address (inferior_ptid,
550 SYMBOL_OBJFILE (var),
551 SYMBOL_VALUE_ADDRESS (var));
552 /* It wouldn't be wrong here to try a gdbarch method, too;
553 finding TLS is an ABI-specific thing. But we don't do that
554 yet. */
555 else
556 error ("Cannot find thread-local variables on this target");
557 break;
558 }
559
c906108c
SS
560 case LOC_TYPEDEF:
561 error ("Cannot look up value of a typedef");
562 break;
563
564 case LOC_BLOCK:
565 if (overlay_debugging)
c5aa993b 566 VALUE_ADDRESS (v) = symbol_overlayed_address
c906108c
SS
567 (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
568 else
569 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
570 return v;
571
572 case LOC_REGISTER:
573 case LOC_REGPARM:
574 case LOC_REGPARM_ADDR:
575 {
576 struct block *b;
577 int regno = SYMBOL_VALUE (var);
3d6d86c6 578 struct value *regval;
c906108c
SS
579
580 if (frame == NULL)
581 return 0;
ae767bfb 582 b = get_frame_block (frame, 0);
c906108c
SS
583
584 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
585 {
586 regval = value_from_register (lookup_pointer_type (type),
c5aa993b 587 regno,
c906108c
SS
588 frame);
589
590 if (regval == NULL)
591 error ("Value of register variable not available.");
592
1aa20aa8 593 addr = value_as_address (regval);
c906108c
SS
594 VALUE_LVAL (v) = lval_memory;
595 }
596 else
597 {
598 regval = value_from_register (type, regno, frame);
599
600 if (regval == NULL)
601 error ("Value of register variable not available.");
602 return regval;
603 }
604 }
605 break;
606
607 case LOC_UNRESOLVED:
608 {
609 struct minimal_symbol *msym;
610
611 msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
612 if (msym == NULL)
613 return 0;
614 if (overlay_debugging)
615 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
616 SYMBOL_BFD_SECTION (msym));
617 else
618 addr = SYMBOL_VALUE_ADDRESS (msym);
619 }
620 break;
621
622 case LOC_OPTIMIZED_OUT:
623 VALUE_LVAL (v) = not_lval;
624 VALUE_OPTIMIZED_OUT (v) = 1;
625 return v;
626
627 default:
628 error ("Cannot look up value of a botched symbol.");
629 break;
630 }
631
632 VALUE_ADDRESS (v) = addr;
633 VALUE_LAZY (v) = 1;
634 return v;
635}
636
637/* Return a value of type TYPE, stored in register REGNUM, in frame
0f2c5ba5 638 FRAME.
c906108c
SS
639
640 NOTE: returns NULL if register value is not available.
641 Caller will check return value or die! */
642
3d6d86c6 643struct value *
fba45db2 644value_from_register (struct type *type, int regnum, struct frame_info *frame)
c906108c 645{
e6cbd02a 646 char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
647 CORE_ADDR addr;
648 int optim;
3d6d86c6 649 struct value *v = allocate_value (type);
c906108c
SS
650 char *value_bytes = 0;
651 int value_bytes_copied = 0;
652 int num_storage_locs;
653 enum lval_type lval;
654 int len;
655
656 CHECK_TYPEDEF (type);
657 len = TYPE_LENGTH (type);
658
659 VALUE_REGNO (v) = regnum;
660
661 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
662 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
663 1);
664
665 if (num_storage_locs > 1
666#ifdef GDB_TARGET_IS_H8500
667 || TYPE_CODE (type) == TYPE_CODE_PTR
668#endif
c5aa993b 669 )
c906108c
SS
670 {
671 /* Value spread across multiple storage locations. */
c5aa993b 672
c906108c
SS
673 int local_regnum;
674 int mem_stor = 0, reg_stor = 0;
675 int mem_tracking = 1;
676 CORE_ADDR last_addr = 0;
677 CORE_ADDR first_addr = 0;
678
679 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
680
681 /* Copy all of the data out, whereever it may be. */
682
683#ifdef GDB_TARGET_IS_H8500
684/* This piece of hideosity is required because the H8500 treats registers
685 differently depending upon whether they are used as pointers or not. As a
686 pointer, a register needs to have a page register tacked onto the front.
687 An alternate way to do this would be to have gcc output different register
688 numbers for the pointer & non-pointer form of the register. But, it
689 doesn't, so we're stuck with this. */
690
691 if (TYPE_CODE (type) == TYPE_CODE_PTR
692 && len > 2)
693 {
694 int page_regnum;
695
696 switch (regnum)
697 {
c5aa993b
JM
698 case R0_REGNUM:
699 case R1_REGNUM:
700 case R2_REGNUM:
701 case R3_REGNUM:
c906108c
SS
702 page_regnum = SEG_D_REGNUM;
703 break;
c5aa993b
JM
704 case R4_REGNUM:
705 case R5_REGNUM:
c906108c
SS
706 page_regnum = SEG_E_REGNUM;
707 break;
c5aa993b
JM
708 case R6_REGNUM:
709 case R7_REGNUM:
c906108c
SS
710 page_regnum = SEG_T_REGNUM;
711 break;
712 }
713
714 value_bytes[0] = 0;
715 get_saved_register (value_bytes + 1,
716 &optim,
717 &addr,
718 frame,
719 page_regnum,
720 &lval);
721
32178cab 722 if (register_cached (page_regnum) == -1)
c906108c
SS
723 return NULL; /* register value not available */
724
725 if (lval == lval_register)
726 reg_stor++;
727 else
728 mem_stor++;
729 first_addr = addr;
730 last_addr = addr;
731
732 get_saved_register (value_bytes + 2,
733 &optim,
734 &addr,
735 frame,
736 regnum,
737 &lval);
738
32178cab 739 if (register_cached (regnum) == -1)
c906108c
SS
740 return NULL; /* register value not available */
741
742 if (lval == lval_register)
743 reg_stor++;
744 else
745 {
746 mem_stor++;
747 mem_tracking = mem_tracking && (addr == last_addr);
748 }
749 last_addr = addr;
750 }
751 else
c5aa993b 752#endif /* GDB_TARGET_IS_H8500 */
c906108c
SS
753 for (local_regnum = regnum;
754 value_bytes_copied < len;
755 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
756 ++local_regnum))
757 {
758 get_saved_register (value_bytes + value_bytes_copied,
759 &optim,
760 &addr,
761 frame,
762 local_regnum,
763 &lval);
764
32178cab 765 if (register_cached (local_regnum) == -1)
c5aa993b 766 return NULL; /* register value not available */
c906108c
SS
767
768 if (regnum == local_regnum)
769 first_addr = addr;
770 if (lval == lval_register)
771 reg_stor++;
772 else
773 {
774 mem_stor++;
c5aa993b 775
c906108c
SS
776 mem_tracking =
777 (mem_tracking
778 && (regnum == local_regnum
779 || addr == last_addr));
780 }
781 last_addr = addr;
782 }
783
784 if ((reg_stor && mem_stor)
785 || (mem_stor && !mem_tracking))
786 /* Mixed storage; all of the hassle we just went through was
787 for some good purpose. */
788 {
789 VALUE_LVAL (v) = lval_reg_frame_relative;
c193f6ac 790 VALUE_FRAME (v) = get_frame_base (frame);
c906108c
SS
791 VALUE_FRAME_REGNUM (v) = regnum;
792 }
793 else if (mem_stor)
794 {
795 VALUE_LVAL (v) = lval_memory;
796 VALUE_ADDRESS (v) = first_addr;
797 }
798 else if (reg_stor)
799 {
800 VALUE_LVAL (v) = lval_register;
801 VALUE_ADDRESS (v) = first_addr;
802 }
803 else
8e65ff28
AC
804 internal_error (__FILE__, __LINE__,
805 "value_from_register: Value not stored anywhere!");
c906108c
SS
806
807 VALUE_OPTIMIZED_OUT (v) = optim;
808
809 /* Any structure stored in more than one register will always be
c5aa993b
JM
810 an integral number of registers. Otherwise, you'd need to do
811 some fiddling with the last register copied here for little
812 endian machines. */
c906108c
SS
813
814 /* Copy into the contents section of the value. */
815 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
816
817 /* Finally do any conversion necessary when extracting this
818 type from more than one register. */
819#ifdef REGISTER_CONVERT_TO_TYPE
c5aa993b 820 REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v));
c906108c
SS
821#endif
822 return v;
823 }
824
825 /* Data is completely contained within a single register. Locate the
826 register's contents in a real register or in core;
827 read the data in raw format. */
828
829 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
830
32178cab 831 if (register_cached (regnum) == -1)
c5aa993b 832 return NULL; /* register value not available */
c906108c
SS
833
834 VALUE_OPTIMIZED_OUT (v) = optim;
835 VALUE_LVAL (v) = lval;
836 VALUE_ADDRESS (v) = addr;
837
13d01224
AC
838 /* Convert the raw register to the corresponding data value's memory
839 format, if necessary. */
c5aa993b 840
13d01224 841 if (CONVERT_REGISTER_P (regnum))
c906108c 842 {
13d01224 843 REGISTER_TO_VALUE (regnum, type, raw_buffer, VALUE_CONTENTS_RAW (v));
c906108c
SS
844 }
845 else
c906108c
SS
846 {
847 /* Raw and virtual formats are the same for this register. */
848
d7449b42 849 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG && len < REGISTER_RAW_SIZE (regnum))
c906108c 850 {
c5aa993b 851 /* Big-endian, and we want less than full size. */
c906108c
SS
852 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
853 }
854
855 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
856 }
c5aa993b 857
c906108c
SS
858 return v;
859}
860\f
861/* Given a struct symbol for a variable or function,
862 and a stack frame id,
863 return a (pointer to a) struct value containing the properly typed
864 address. */
865
3d6d86c6 866struct value *
fba45db2 867locate_var_value (register struct symbol *var, struct frame_info *frame)
c906108c
SS
868{
869 CORE_ADDR addr = 0;
870 struct type *type = SYMBOL_TYPE (var);
3d6d86c6 871 struct value *lazy_value;
c906108c
SS
872
873 /* Evaluate it first; if the result is a memory address, we're fine.
874 Lazy evaluation pays off here. */
875
876 lazy_value = read_var_value (var, frame);
877 if (lazy_value == 0)
878 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
879
880 if (VALUE_LAZY (lazy_value)
881 || TYPE_CODE (type) == TYPE_CODE_FUNC)
882 {
3d6d86c6 883 struct value *val;
c906108c
SS
884
885 addr = VALUE_ADDRESS (lazy_value);
4478b372 886 val = value_from_pointer (lookup_pointer_type (type), addr);
c906108c
SS
887 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
888 return val;
889 }
890
891 /* Not a memory address; check what the problem was. */
c5aa993b 892 switch (VALUE_LVAL (lazy_value))
c906108c
SS
893 {
894 case lval_register:
14e534aa
PM
895 gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value)) != NULL
896 && *REGISTER_NAME (VALUE_REGNO (lazy_value)) != '\0');
897 error("Address requested for identifier "
898 "\"%s\" which is in register $%s",
899 SYMBOL_SOURCE_NAME (var),
900 REGISTER_NAME (VALUE_REGNO (lazy_value)));
901 break;
902
c906108c 903 case lval_reg_frame_relative:
14e534aa
PM
904 gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != NULL
905 && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != '\0');
906 error("Address requested for identifier "
907 "\"%s\" which is in frame register $%s",
908 SYMBOL_SOURCE_NAME (var),
909 REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)));
c906108c
SS
910 break;
911
912 default:
913 error ("Can't take address of \"%s\" which isn't an lvalue.",
914 SYMBOL_SOURCE_NAME (var));
915 break;
916 }
c5aa993b 917 return 0; /* For lint -- never reached */
c906108c 918}
This page took 0.516756 seconds and 4 git commands to generate.