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