Based on patches from H.J. Lu <hjl@nynexst.com>:
[deliverable/binutils-gdb.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "target.h"
28 #include <string.h>
29
30 static void write_register_pid PARAMS ((int regno, LONGEST val, int pid));
31
32 /* Basic byte-swapping routines. GDB has needed these for a long time...
33 All extract a target-format integer at ADDR which is LEN bytes long. */
34
35 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
36 /* 8 bit characters are a pretty safe assumption these days, so we
37 assume it throughout all these swapping routines. If we had to deal with
38 9 bit characters, we would need to make len be in bits and would have
39 to re-write these routines... */
40 you lose
41 #endif
42
43 LONGEST
44 extract_signed_integer (addr, len)
45 PTR addr;
46 int len;
47 {
48 LONGEST retval;
49 unsigned char *p;
50 unsigned char *startaddr = (unsigned char *)addr;
51 unsigned char *endaddr = startaddr + len;
52
53 if (len > sizeof (LONGEST))
54 error ("\
55 That operation is not available on integers of more than %d bytes.",
56 sizeof (LONGEST));
57
58 /* Start at the most significant end of the integer, and work towards
59 the least significant. */
60 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
61 {
62 p = startaddr;
63 /* Do the sign extension once at the start. */
64 retval = ((LONGEST)*p ^ 0x80) - 0x80;
65 for (++p; p < endaddr; ++p)
66 retval = (retval << 8) | *p;
67 }
68 else
69 {
70 p = endaddr - 1;
71 /* Do the sign extension once at the start. */
72 retval = ((LONGEST)*p ^ 0x80) - 0x80;
73 for (--p; p >= startaddr; --p)
74 retval = (retval << 8) | *p;
75 }
76 return retval;
77 }
78
79 unsigned LONGEST
80 extract_unsigned_integer (addr, len)
81 PTR addr;
82 int len;
83 {
84 unsigned LONGEST retval;
85 unsigned char *p;
86 unsigned char *startaddr = (unsigned char *)addr;
87 unsigned char *endaddr = startaddr + len;
88
89 if (len > sizeof (unsigned LONGEST))
90 error ("\
91 That operation is not available on integers of more than %d bytes.",
92 sizeof (unsigned LONGEST));
93
94 /* Start at the most significant end of the integer, and work towards
95 the least significant. */
96 retval = 0;
97 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
98 {
99 for (p = startaddr; p < endaddr; ++p)
100 retval = (retval << 8) | *p;
101 }
102 else
103 {
104 for (p = endaddr - 1; p >= startaddr; --p)
105 retval = (retval << 8) | *p;
106 }
107 return retval;
108 }
109
110 CORE_ADDR
111 extract_address (addr, len)
112 PTR addr;
113 int len;
114 {
115 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
116 whether we want this to be true eventually. */
117 return extract_unsigned_integer (addr, len);
118 }
119
120 void
121 store_signed_integer (addr, len, val)
122 PTR addr;
123 int len;
124 LONGEST val;
125 {
126 unsigned char *p;
127 unsigned char *startaddr = (unsigned char *)addr;
128 unsigned char *endaddr = startaddr + len;
129
130 /* Start at the least significant end of the integer, and work towards
131 the most significant. */
132 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
133 {
134 for (p = endaddr - 1; p >= startaddr; --p)
135 {
136 *p = val & 0xff;
137 val >>= 8;
138 }
139 }
140 else
141 {
142 for (p = startaddr; p < endaddr; ++p)
143 {
144 *p = val & 0xff;
145 val >>= 8;
146 }
147 }
148 }
149
150 void
151 store_unsigned_integer (addr, len, val)
152 PTR addr;
153 int len;
154 unsigned LONGEST val;
155 {
156 unsigned char *p;
157 unsigned char *startaddr = (unsigned char *)addr;
158 unsigned char *endaddr = startaddr + len;
159
160 /* Start at the least significant end of the integer, and work towards
161 the most significant. */
162 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
163 {
164 for (p = endaddr - 1; p >= startaddr; --p)
165 {
166 *p = val & 0xff;
167 val >>= 8;
168 }
169 }
170 else
171 {
172 for (p = startaddr; p < endaddr; ++p)
173 {
174 *p = val & 0xff;
175 val >>= 8;
176 }
177 }
178 }
179
180 void
181 store_address (addr, len, val)
182 PTR addr;
183 int len;
184 CORE_ADDR val;
185 {
186 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
187 whether we want this to be true eventually. */
188 store_unsigned_integer (addr, len, (LONGEST)val);
189 }
190 \f
191 /* Swap LEN bytes at BUFFER between target and host byte-order. */
192 #define SWAP_FLOATING(buffer,len) \
193 do \
194 { \
195 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
196 { \
197 char tmp; \
198 char *p = (char *)(buffer); \
199 char *q = ((char *)(buffer)) + len - 1; \
200 for (; p < q; p++, q--) \
201 { \
202 tmp = *q; \
203 *q = *p; \
204 *p = tmp; \
205 } \
206 } \
207 } \
208 while (0)
209
210 /* There are various problems with the extract_floating and store_floating
211 routines.
212
213 1. These routines only handle byte-swapping, not conversion of
214 formats. So if host is IEEE floating and target is VAX floating,
215 or vice-versa, it loses. This means that we can't (yet) use these
216 routines for extendeds. Extendeds are handled by
217 REGISTER_CONVERTIBLE. What we want is to use floatformat.h, but that
218 doesn't yet handle VAX floating at all.
219
220 2. We can't deal with it if there is more than one floating point
221 format in use. This has to be fixed at the unpack_double level.
222
223 3. We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
224 we want to call it which is long double where available. */
225
226 double
227 extract_floating (addr, len)
228 PTR addr;
229 int len;
230 {
231 if (len == sizeof (float))
232 {
233 float retval;
234 memcpy (&retval, addr, sizeof (retval));
235 SWAP_FLOATING (&retval, sizeof (retval));
236 return retval;
237 }
238 else if (len == sizeof (double))
239 {
240 double retval;
241 memcpy (&retval, addr, sizeof (retval));
242 SWAP_FLOATING (&retval, sizeof (retval));
243 return retval;
244 }
245 else
246 {
247 error ("Can't deal with a floating point number of %d bytes.", len);
248 }
249 }
250
251 void
252 store_floating (addr, len, val)
253 PTR addr;
254 int len;
255 double val;
256 {
257 if (len == sizeof (float))
258 {
259 float floatval = val;
260 SWAP_FLOATING (&floatval, sizeof (floatval));
261 memcpy (addr, &floatval, sizeof (floatval));
262 }
263 else if (len == sizeof (double))
264 {
265 SWAP_FLOATING (&val, sizeof (val));
266 memcpy (addr, &val, sizeof (val));
267 }
268 else
269 {
270 error ("Can't deal with a floating point number of %d bytes.", len);
271 }
272 }
273 \f
274 #if !defined (GET_SAVED_REGISTER)
275
276 /* Return the address in which frame FRAME's value of register REGNUM
277 has been saved in memory. Or return zero if it has not been saved.
278 If REGNUM specifies the SP, the value we return is actually
279 the SP value, not an address where it was saved. */
280
281 CORE_ADDR
282 find_saved_register (frame, regnum)
283 struct frame_info *frame;
284 int regnum;
285 {
286 struct frame_saved_regs saved_regs;
287
288 register struct frame_info *frame1 = NULL;
289 register CORE_ADDR addr = 0;
290
291 if (frame == NULL) /* No regs saved if want current frame */
292 return 0;
293
294 #ifdef HAVE_REGISTER_WINDOWS
295 /* We assume that a register in a register window will only be saved
296 in one place (since the name changes and/or disappears as you go
297 towards inner frames), so we only call get_frame_saved_regs on
298 the current frame. This is directly in contradiction to the
299 usage below, which assumes that registers used in a frame must be
300 saved in a lower (more interior) frame. This change is a result
301 of working on a register window machine; get_frame_saved_regs
302 always returns the registers saved within a frame, within the
303 context (register namespace) of that frame. */
304
305 /* However, note that we don't want this to return anything if
306 nothing is saved (if there's a frame inside of this one). Also,
307 callers to this routine asking for the stack pointer want the
308 stack pointer saved for *this* frame; this is returned from the
309 next frame. */
310
311 if (REGISTER_IN_WINDOW_P(regnum))
312 {
313 frame1 = get_next_frame (frame);
314 if (!frame1) return 0; /* Registers of this frame are active. */
315
316 /* Get the SP from the next frame in; it will be this
317 current frame. */
318 if (regnum != SP_REGNUM)
319 frame1 = frame;
320
321 get_frame_saved_regs (frame1, &saved_regs);
322 return saved_regs.regs[regnum]; /* ... which might be zero */
323 }
324 #endif /* HAVE_REGISTER_WINDOWS */
325
326 /* Note that this next routine assumes that registers used in
327 frame x will be saved only in the frame that x calls and
328 frames interior to it. This is not true on the sparc, but the
329 above macro takes care of it, so we should be all right. */
330 while (1)
331 {
332 QUIT;
333 frame1 = get_prev_frame (frame1);
334 if (frame1 == 0 || frame1 == frame)
335 break;
336 get_frame_saved_regs (frame1, &saved_regs);
337 if (saved_regs.regs[regnum])
338 addr = saved_regs.regs[regnum];
339 }
340
341 return addr;
342 }
343
344 /* Find register number REGNUM relative to FRAME and put its (raw,
345 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
346 variable was optimized out (and thus can't be fetched). Set *LVAL
347 to lval_memory, lval_register, or not_lval, depending on whether
348 the value was fetched from memory, from a register, or in a strange
349 and non-modifiable way (e.g. a frame pointer which was calculated
350 rather than fetched). Set *ADDRP to the address, either in memory
351 on as a REGISTER_BYTE offset into the registers array.
352
353 Note that this implementation never sets *LVAL to not_lval. But
354 it can be replaced by defining GET_SAVED_REGISTER and supplying
355 your own.
356
357 The argument RAW_BUFFER must point to aligned memory. */
358
359 void
360 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
361 char *raw_buffer;
362 int *optimized;
363 CORE_ADDR *addrp;
364 struct frame_info *frame;
365 int regnum;
366 enum lval_type *lval;
367 {
368 CORE_ADDR addr;
369
370 if (!target_has_registers)
371 error ("No registers.");
372
373 /* Normal systems don't optimize out things with register numbers. */
374 if (optimized != NULL)
375 *optimized = 0;
376 addr = find_saved_register (frame, regnum);
377 if (addr != 0)
378 {
379 if (lval != NULL)
380 *lval = lval_memory;
381 if (regnum == SP_REGNUM)
382 {
383 if (raw_buffer != NULL)
384 {
385 /* Put it back in target format. */
386 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
387 }
388 if (addrp != NULL)
389 *addrp = 0;
390 return;
391 }
392 if (raw_buffer != NULL)
393 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
394 }
395 else
396 {
397 if (lval != NULL)
398 *lval = lval_register;
399 addr = REGISTER_BYTE (regnum);
400 if (raw_buffer != NULL)
401 read_register_gen (regnum, raw_buffer);
402 }
403 if (addrp != NULL)
404 *addrp = addr;
405 }
406 #endif /* GET_SAVED_REGISTER. */
407
408 /* Copy the bytes of register REGNUM, relative to the current stack frame,
409 into our memory at MYADDR, in target byte order.
410 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
411
412 Returns 1 if could not be read, 0 if could. */
413
414 int
415 read_relative_register_raw_bytes (regnum, myaddr)
416 int regnum;
417 char *myaddr;
418 {
419 int optim;
420 if (regnum == FP_REGNUM && selected_frame)
421 {
422 /* Put it back in target format. */
423 store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
424 FRAME_FP(selected_frame));
425 return 0;
426 }
427
428 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
429 regnum, (enum lval_type *)NULL);
430 return optim;
431 }
432
433 /* Return a `value' with the contents of register REGNUM
434 in its virtual format, with the type specified by
435 REGISTER_VIRTUAL_TYPE. */
436
437 value_ptr
438 value_of_register (regnum)
439 int regnum;
440 {
441 CORE_ADDR addr;
442 int optim;
443 register value_ptr reg_val;
444 char raw_buffer[MAX_REGISTER_RAW_SIZE];
445 enum lval_type lval;
446
447 get_saved_register (raw_buffer, &optim, &addr,
448 selected_frame, regnum, &lval);
449
450 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
451
452 /* Convert raw data to virtual format if necessary. */
453
454 #ifdef REGISTER_CONVERTIBLE
455 if (REGISTER_CONVERTIBLE (regnum))
456 {
457 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
458 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
459 }
460 else
461 #endif
462 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
463 REGISTER_RAW_SIZE (regnum));
464 VALUE_LVAL (reg_val) = lval;
465 VALUE_ADDRESS (reg_val) = addr;
466 VALUE_REGNO (reg_val) = regnum;
467 VALUE_OPTIMIZED_OUT (reg_val) = optim;
468 return reg_val;
469 }
470 \f
471 /* Low level examining and depositing of registers.
472
473 The caller is responsible for making
474 sure that the inferior is stopped before calling the fetching routines,
475 or it will get garbage. (a change from GDB version 3, in which
476 the caller got the value from the last stop). */
477
478 /* Contents of the registers in target byte order.
479 We allocate some extra slop since we do a lot of memcpy's around `registers',
480 and failing-soft is better than failing hard. */
481 char registers[REGISTER_BYTES + /* SLOP */ 256];
482
483 /* Nonzero if that register has been fetched. */
484 char register_valid[NUM_REGS];
485
486 /* The thread/process associated with the current set of registers. For now,
487 -1 is special, and means `no current process'. */
488 int registers_pid = -1;
489
490 /* Indicate that registers may have changed, so invalidate the cache. */
491
492 void
493 registers_changed ()
494 {
495 int i;
496 int numregs = ARCH_NUM_REGS;
497
498 registers_pid = -1;
499
500 for (i = 0; i < numregs; i++)
501 register_valid[i] = 0;
502 }
503
504 /* Indicate that all registers have been fetched, so mark them all valid. */
505 void
506 registers_fetched ()
507 {
508 int i;
509 int numregs = ARCH_NUM_REGS;
510 for (i = 0; i < numregs; i++)
511 register_valid[i] = 1;
512 }
513
514 /* Copy LEN bytes of consecutive data from registers
515 starting with the REGBYTE'th byte of register data
516 into memory at MYADDR. */
517
518 void
519 read_register_bytes (regbyte, myaddr, len)
520 int regbyte;
521 char *myaddr;
522 int len;
523 {
524 /* Fetch all registers. */
525 int i, numregs;
526
527 if (registers_pid != inferior_pid)
528 {
529 registers_changed ();
530 registers_pid = inferior_pid;
531 }
532
533 numregs = ARCH_NUM_REGS;
534 for (i = 0; i < numregs; i++)
535 if (!register_valid[i])
536 {
537 target_fetch_registers (-1);
538 break;
539 }
540 if (myaddr != NULL)
541 memcpy (myaddr, &registers[regbyte], len);
542 }
543
544 /* Read register REGNO into memory at MYADDR, which must be large enough
545 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
546 If the register is known to be the size of a CORE_ADDR or smaller,
547 read_register can be used instead. */
548 void
549 read_register_gen (regno, myaddr)
550 int regno;
551 char *myaddr;
552 {
553 if (registers_pid != inferior_pid)
554 {
555 registers_changed ();
556 registers_pid = inferior_pid;
557 }
558
559 if (!register_valid[regno])
560 target_fetch_registers (regno);
561 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
562 REGISTER_RAW_SIZE (regno));
563 }
564
565 /* Copy LEN bytes of consecutive data from memory at MYADDR
566 into registers starting with the REGBYTE'th byte of register data. */
567
568 void
569 write_register_bytes (regbyte, myaddr, len)
570 int regbyte;
571 char *myaddr;
572 int len;
573 {
574 if (registers_pid != inferior_pid)
575 {
576 registers_changed ();
577 registers_pid = inferior_pid;
578 }
579
580 /* Make sure the entire registers array is valid. */
581 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
582 memcpy (&registers[regbyte], myaddr, len);
583 target_store_registers (-1);
584 }
585
586 /* Return the raw contents of register REGNO, regarding it as an integer. */
587 /* This probably should be returning LONGEST rather than CORE_ADDR. */
588
589 CORE_ADDR
590 read_register (regno)
591 int regno;
592 {
593 if (registers_pid != inferior_pid)
594 {
595 registers_changed ();
596 registers_pid = inferior_pid;
597 }
598
599 if (!register_valid[regno])
600 target_fetch_registers (regno);
601
602 return extract_address (&registers[REGISTER_BYTE (regno)],
603 REGISTER_RAW_SIZE(regno));
604 }
605
606 CORE_ADDR
607 read_register_pid (regno, pid)
608 int regno, pid;
609 {
610 int save_pid;
611 CORE_ADDR retval;
612
613 if (pid == inferior_pid)
614 return read_register (regno);
615
616 save_pid = inferior_pid;
617
618 inferior_pid = pid;
619
620 retval = read_register (regno);
621
622 inferior_pid = save_pid;
623
624 return retval;
625 }
626
627 /* Registers we shouldn't try to store. */
628 #if !defined (CANNOT_STORE_REGISTER)
629 #define CANNOT_STORE_REGISTER(regno) 0
630 #endif
631
632 /* Store VALUE, into the raw contents of register number REGNO. */
633
634 void
635 write_register (regno, val)
636 int regno;
637 LONGEST val;
638 {
639 PTR buf;
640 int size;
641
642 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
643 the registers array if something writes to this register. */
644 if (CANNOT_STORE_REGISTER (regno))
645 return;
646
647 if (registers_pid != inferior_pid)
648 {
649 registers_changed ();
650 registers_pid = inferior_pid;
651 }
652
653 size = REGISTER_RAW_SIZE(regno);
654 buf = alloca (size);
655 store_signed_integer (buf, size, (LONGEST) val);
656
657 /* If we have a valid copy of the register, and new value == old value,
658 then don't bother doing the actual store. */
659
660 if (register_valid [regno]
661 && memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
662 return;
663
664 target_prepare_to_store ();
665
666 memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
667
668 register_valid [regno] = 1;
669
670 target_store_registers (regno);
671 }
672
673 static void
674 write_register_pid (regno, val, pid)
675 int regno;
676 LONGEST val;
677 int pid;
678 {
679 int save_pid;
680
681 if (pid == inferior_pid)
682 {
683 write_register (regno, val);
684 return;
685 }
686
687 save_pid = inferior_pid;
688
689 inferior_pid = pid;
690
691 write_register (regno, val);
692
693 inferior_pid = save_pid;
694 }
695
696 /* Record that register REGNO contains VAL.
697 This is used when the value is obtained from the inferior or core dump,
698 so there is no need to store the value there. */
699
700 void
701 supply_register (regno, val)
702 int regno;
703 char *val;
704 {
705 if (registers_pid != inferior_pid)
706 {
707 registers_changed ();
708 registers_pid = inferior_pid;
709 }
710
711 register_valid[regno] = 1;
712 memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
713
714 /* On some architectures, e.g. HPPA, there are a few stray bits in some
715 registers, that the rest of the code would like to ignore. */
716 #ifdef CLEAN_UP_REGISTER_VALUE
717 CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
718 #endif
719 }
720
721
722 /* This routine is getting awfully cluttered with #if's. It's probably
723 time to turn this into READ_PC and define it in the tm.h file.
724 Ditto for write_pc. */
725
726 CORE_ADDR
727 read_pc ()
728 {
729 #ifdef TARGET_READ_PC
730 return TARGET_READ_PC (inferior_pid);
731 #else
732 return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, inferior_pid));
733 #endif
734 }
735
736 CORE_ADDR
737 read_pc_pid (pid)
738 int pid;
739 {
740 #ifdef TARGET_READ_PC
741 return TARGET_READ_PC (pid);
742 #else
743 return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
744 #endif
745 }
746
747 void
748 write_pc (val)
749 CORE_ADDR val;
750 {
751 #ifdef TARGET_WRITE_PC
752 TARGET_WRITE_PC (val, inferior_pid);
753 #else
754 write_register_pid (PC_REGNUM, val, inferior_pid);
755 #ifdef NPC_REGNUM
756 write_register_pid (NPC_REGNUM, val + 4, inferior_pid);
757 #ifdef NNPC_REGNUM
758 write_register_pid (NNPC_REGNUM, val + 8, inferior_pid);
759 #endif
760 #endif
761 #endif
762 }
763
764 void
765 write_pc_pid (val, pid)
766 CORE_ADDR val;
767 int pid;
768 {
769 #ifdef TARGET_WRITE_PC
770 TARGET_WRITE_PC (val, pid);
771 #else
772 write_register_pid (PC_REGNUM, val, pid);
773 #ifdef NPC_REGNUM
774 write_register_pid (NPC_REGNUM, val + 4, pid);
775 #ifdef NNPC_REGNUM
776 write_register_pid (NNPC_REGNUM, val + 8, pid);
777 #endif
778 #endif
779 #endif
780 }
781
782 /* Cope with strage ways of getting to the stack and frame pointers */
783
784 CORE_ADDR
785 read_sp ()
786 {
787 #ifdef TARGET_READ_SP
788 return TARGET_READ_SP ();
789 #else
790 return read_register (SP_REGNUM);
791 #endif
792 }
793
794 void
795 write_sp (val)
796 CORE_ADDR val;
797 {
798 #ifdef TARGET_WRITE_SP
799 TARGET_WRITE_SP (val);
800 #else
801 write_register (SP_REGNUM, val);
802 #endif
803 }
804
805 CORE_ADDR
806 read_fp ()
807 {
808 #ifdef TARGET_READ_FP
809 return TARGET_READ_FP ();
810 #else
811 return read_register (FP_REGNUM);
812 #endif
813 }
814
815 void
816 write_fp (val)
817 CORE_ADDR val;
818 {
819 #ifdef TARGET_WRITE_FP
820 TARGET_WRITE_FP (val);
821 #else
822 write_register (FP_REGNUM, val);
823 #endif
824 }
825 \f
826 /* Will calling read_var_value or locate_var_value on SYM end
827 up caring what frame it is being evaluated relative to? SYM must
828 be non-NULL. */
829 int
830 symbol_read_needs_frame (sym)
831 struct symbol *sym;
832 {
833 switch (SYMBOL_CLASS (sym))
834 {
835 /* All cases listed explicitly so that gcc -Wall will detect it if
836 we failed to consider one. */
837 case LOC_REGISTER:
838 case LOC_ARG:
839 case LOC_REF_ARG:
840 case LOC_REGPARM:
841 case LOC_REGPARM_ADDR:
842 case LOC_LOCAL:
843 case LOC_LOCAL_ARG:
844 case LOC_BASEREG:
845 case LOC_BASEREG_ARG:
846 return 1;
847
848 case LOC_UNDEF:
849 case LOC_CONST:
850 case LOC_STATIC:
851 case LOC_TYPEDEF:
852
853 case LOC_LABEL:
854 /* Getting the address of a label can be done independently of the block,
855 even if some *uses* of that address wouldn't work so well without
856 the right frame. */
857
858 case LOC_BLOCK:
859 case LOC_CONST_BYTES:
860 case LOC_OPTIMIZED_OUT:
861 return 0;
862 }
863 return 1;
864 }
865
866 /* Given a struct symbol for a variable,
867 and a stack frame id, read the value of the variable
868 and return a (pointer to a) struct value containing the value.
869 If the variable cannot be found, return a zero pointer.
870 If FRAME is NULL, use the selected_frame. */
871
872 value_ptr
873 read_var_value (var, frame)
874 register struct symbol *var;
875 struct frame_info *frame;
876 {
877 register value_ptr v;
878 struct type *type = SYMBOL_TYPE (var);
879 CORE_ADDR addr;
880 register int len;
881
882 v = allocate_value (type);
883 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
884 len = TYPE_LENGTH (type);
885
886 if (frame == NULL) frame = selected_frame;
887
888 switch (SYMBOL_CLASS (var))
889 {
890 case LOC_CONST:
891 /* Put the constant back in target format. */
892 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
893 (LONGEST) SYMBOL_VALUE (var));
894 VALUE_LVAL (v) = not_lval;
895 return v;
896
897 case LOC_LABEL:
898 /* Put the constant back in target format. */
899 store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
900 VALUE_LVAL (v) = not_lval;
901 return v;
902
903 case LOC_CONST_BYTES:
904 {
905 char *bytes_addr;
906 bytes_addr = SYMBOL_VALUE_BYTES (var);
907 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
908 VALUE_LVAL (v) = not_lval;
909 return v;
910 }
911
912 case LOC_STATIC:
913 addr = SYMBOL_VALUE_ADDRESS (var);
914 break;
915
916 case LOC_ARG:
917 if (frame == NULL)
918 return 0;
919 addr = FRAME_ARGS_ADDRESS (frame);
920 if (!addr)
921 return 0;
922 addr += SYMBOL_VALUE (var);
923 break;
924
925 case LOC_REF_ARG:
926 if (frame == NULL)
927 return 0;
928 addr = FRAME_ARGS_ADDRESS (frame);
929 if (!addr)
930 return 0;
931 addr += SYMBOL_VALUE (var);
932 addr = read_memory_unsigned_integer
933 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
934 break;
935
936 case LOC_LOCAL:
937 case LOC_LOCAL_ARG:
938 if (frame == NULL)
939 return 0;
940 addr = FRAME_LOCALS_ADDRESS (frame);
941 addr += SYMBOL_VALUE (var);
942 break;
943
944 case LOC_BASEREG:
945 case LOC_BASEREG_ARG:
946 {
947 char buf[MAX_REGISTER_RAW_SIZE];
948 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
949 NULL);
950 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
951 addr += SYMBOL_VALUE (var);
952 break;
953 }
954
955 case LOC_TYPEDEF:
956 error ("Cannot look up value of a typedef");
957 break;
958
959 case LOC_BLOCK:
960 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
961 return v;
962
963 case LOC_REGISTER:
964 case LOC_REGPARM:
965 case LOC_REGPARM_ADDR:
966 {
967 struct block *b;
968
969 if (frame == NULL)
970 return 0;
971 b = get_frame_block (frame);
972
973
974 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
975 {
976 addr =
977 value_as_pointer (value_from_register (lookup_pointer_type (type),
978 SYMBOL_VALUE (var),
979 frame));
980 VALUE_LVAL (v) = lval_memory;
981 }
982 else
983 return value_from_register (type, SYMBOL_VALUE (var), frame);
984 }
985 break;
986
987 case LOC_OPTIMIZED_OUT:
988 VALUE_LVAL (v) = not_lval;
989 VALUE_OPTIMIZED_OUT (v) = 1;
990 return v;
991
992 default:
993 error ("Cannot look up value of a botched symbol.");
994 break;
995 }
996
997 VALUE_ADDRESS (v) = addr;
998 VALUE_LAZY (v) = 1;
999 return v;
1000 }
1001
1002 /* Return a value of type TYPE, stored in register REGNUM, in frame
1003 FRAME. */
1004
1005 value_ptr
1006 value_from_register (type, regnum, frame)
1007 struct type *type;
1008 int regnum;
1009 struct frame_info *frame;
1010 {
1011 char raw_buffer [MAX_REGISTER_RAW_SIZE];
1012 CORE_ADDR addr;
1013 int optim;
1014 value_ptr v = allocate_value (type);
1015 int len = TYPE_LENGTH (type);
1016 char *value_bytes = 0;
1017 int value_bytes_copied = 0;
1018 int num_storage_locs;
1019 enum lval_type lval;
1020
1021 VALUE_REGNO (v) = regnum;
1022
1023 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
1024 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
1025 1);
1026
1027 if (num_storage_locs > 1
1028 #ifdef GDB_TARGET_IS_H8500
1029 || TYPE_CODE (type) == TYPE_CODE_PTR
1030 #endif
1031 )
1032 {
1033 /* Value spread across multiple storage locations. */
1034
1035 int local_regnum;
1036 int mem_stor = 0, reg_stor = 0;
1037 int mem_tracking = 1;
1038 CORE_ADDR last_addr = 0;
1039 CORE_ADDR first_addr = 0;
1040
1041 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1042
1043 /* Copy all of the data out, whereever it may be. */
1044
1045 #ifdef GDB_TARGET_IS_H8500
1046 /* This piece of hideosity is required because the H8500 treats registers
1047 differently depending upon whether they are used as pointers or not. As a
1048 pointer, a register needs to have a page register tacked onto the front.
1049 An alternate way to do this would be to have gcc output different register
1050 numbers for the pointer & non-pointer form of the register. But, it
1051 doesn't, so we're stuck with this. */
1052
1053 if (TYPE_CODE (type) == TYPE_CODE_PTR
1054 && len > 2)
1055 {
1056 int page_regnum;
1057
1058 switch (regnum)
1059 {
1060 case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
1061 page_regnum = SEG_D_REGNUM;
1062 break;
1063 case R4_REGNUM: case R5_REGNUM:
1064 page_regnum = SEG_E_REGNUM;
1065 break;
1066 case R6_REGNUM: case R7_REGNUM:
1067 page_regnum = SEG_T_REGNUM;
1068 break;
1069 }
1070
1071 value_bytes[0] = 0;
1072 get_saved_register (value_bytes + 1,
1073 &optim,
1074 &addr,
1075 frame,
1076 page_regnum,
1077 &lval);
1078
1079 if (lval == lval_register)
1080 reg_stor++;
1081 else
1082 mem_stor++;
1083 first_addr = addr;
1084 last_addr = addr;
1085
1086 get_saved_register (value_bytes + 2,
1087 &optim,
1088 &addr,
1089 frame,
1090 regnum,
1091 &lval);
1092
1093 if (lval == lval_register)
1094 reg_stor++;
1095 else
1096 {
1097 mem_stor++;
1098 mem_tracking = mem_tracking && (addr == last_addr);
1099 }
1100 last_addr = addr;
1101 }
1102 else
1103 #endif /* GDB_TARGET_IS_H8500 */
1104 for (local_regnum = regnum;
1105 value_bytes_copied < len;
1106 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
1107 ++local_regnum))
1108 {
1109 get_saved_register (value_bytes + value_bytes_copied,
1110 &optim,
1111 &addr,
1112 frame,
1113 local_regnum,
1114 &lval);
1115
1116 if (regnum == local_regnum)
1117 first_addr = addr;
1118 if (lval == lval_register)
1119 reg_stor++;
1120 else
1121 {
1122 mem_stor++;
1123
1124 mem_tracking =
1125 (mem_tracking
1126 && (regnum == local_regnum
1127 || addr == last_addr));
1128 }
1129 last_addr = addr;
1130 }
1131
1132 if ((reg_stor && mem_stor)
1133 || (mem_stor && !mem_tracking))
1134 /* Mixed storage; all of the hassle we just went through was
1135 for some good purpose. */
1136 {
1137 VALUE_LVAL (v) = lval_reg_frame_relative;
1138 VALUE_FRAME (v) = FRAME_FP (frame);
1139 VALUE_FRAME_REGNUM (v) = regnum;
1140 }
1141 else if (mem_stor)
1142 {
1143 VALUE_LVAL (v) = lval_memory;
1144 VALUE_ADDRESS (v) = first_addr;
1145 }
1146 else if (reg_stor)
1147 {
1148 VALUE_LVAL (v) = lval_register;
1149 VALUE_ADDRESS (v) = first_addr;
1150 }
1151 else
1152 fatal ("value_from_register: Value not stored anywhere!");
1153
1154 VALUE_OPTIMIZED_OUT (v) = optim;
1155
1156 /* Any structure stored in more than one register will always be
1157 an integral number of registers. Otherwise, you'd need to do
1158 some fiddling with the last register copied here for little
1159 endian machines. */
1160
1161 /* Copy into the contents section of the value. */
1162 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
1163
1164 /* Finally do any conversion necessary when extracting this
1165 type from more than one register. */
1166 #ifdef REGISTER_CONVERT_TO_TYPE
1167 REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
1168 #endif
1169 return v;
1170 }
1171
1172 /* Data is completely contained within a single register. Locate the
1173 register's contents in a real register or in core;
1174 read the data in raw format. */
1175
1176 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
1177 VALUE_OPTIMIZED_OUT (v) = optim;
1178 VALUE_LVAL (v) = lval;
1179 VALUE_ADDRESS (v) = addr;
1180
1181 /* Convert raw data to virtual format if necessary. */
1182
1183 #ifdef REGISTER_CONVERTIBLE
1184 if (REGISTER_CONVERTIBLE (regnum))
1185 {
1186 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1187 raw_buffer, VALUE_CONTENTS_RAW (v));
1188 }
1189 else
1190 #endif
1191 {
1192 /* Raw and virtual formats are the same for this register. */
1193
1194 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
1195 {
1196 /* Big-endian, and we want less than full size. */
1197 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1198 }
1199
1200 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
1201 }
1202
1203 return v;
1204 }
1205 \f
1206 /* Given a struct symbol for a variable or function,
1207 and a stack frame id,
1208 return a (pointer to a) struct value containing the properly typed
1209 address. */
1210
1211 value_ptr
1212 locate_var_value (var, frame)
1213 register struct symbol *var;
1214 struct frame_info *frame;
1215 {
1216 CORE_ADDR addr = 0;
1217 struct type *type = SYMBOL_TYPE (var);
1218 value_ptr lazy_value;
1219
1220 /* Evaluate it first; if the result is a memory address, we're fine.
1221 Lazy evaluation pays off here. */
1222
1223 lazy_value = read_var_value (var, frame);
1224 if (lazy_value == 0)
1225 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1226
1227 if (VALUE_LAZY (lazy_value)
1228 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1229 {
1230 addr = VALUE_ADDRESS (lazy_value);
1231 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
1232 }
1233
1234 /* Not a memory address; check what the problem was. */
1235 switch (VALUE_LVAL (lazy_value))
1236 {
1237 case lval_register:
1238 case lval_reg_frame_relative:
1239 error ("Address requested for identifier \"%s\" which is in a register.",
1240 SYMBOL_SOURCE_NAME (var));
1241 break;
1242
1243 default:
1244 error ("Can't take address of \"%s\" which isn't an lvalue.",
1245 SYMBOL_SOURCE_NAME (var));
1246 break;
1247 }
1248 return 0; /* For lint -- never reached */
1249 }
This page took 0.060864 seconds and 4 git commands to generate.