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