* Makefile.in (VERSION): Bump to 4.9.2.
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
bd5635a1 1/* Find a variable's value in memory, for GDB, the GNU debugger.
7d9884b9 2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
36b9d39c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
36b9d39c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
36b9d39c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
36b9d39c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1 21#include "symtab.h"
51b57ded 22#include "gdbtypes.h"
bd5635a1
RP
23#include "frame.h"
24#include "value.h"
25#include "gdbcore.h"
26#include "inferior.h"
27#include "target.h"
28
29#if !defined (GET_SAVED_REGISTER)
30
31/* Return the address in which frame FRAME's value of register REGNUM
32 has been saved in memory. Or return zero if it has not been saved.
33 If REGNUM specifies the SP, the value we return is actually
34 the SP value, not an address where it was saved. */
35
36CORE_ADDR
37find_saved_register (frame, regnum)
38 FRAME frame;
39 int regnum;
40{
41 struct frame_info *fi;
42 struct frame_saved_regs saved_regs;
43
44 register FRAME frame1 = 0;
45 register CORE_ADDR addr = 0;
46
47 if (frame == 0) /* No regs saved if want current frame */
48 return 0;
49
50#ifdef HAVE_REGISTER_WINDOWS
51 /* We assume that a register in a register window will only be saved
52 in one place (since the name changes and/or disappears as you go
53 towards inner frames), so we only call get_frame_saved_regs on
54 the current frame. This is directly in contradiction to the
55 usage below, which assumes that registers used in a frame must be
56 saved in a lower (more interior) frame. This change is a result
57 of working on a register window machine; get_frame_saved_regs
58 always returns the registers saved within a frame, within the
59 context (register namespace) of that frame. */
60
61 /* However, note that we don't want this to return anything if
62 nothing is saved (if there's a frame inside of this one). Also,
63 callers to this routine asking for the stack pointer want the
64 stack pointer saved for *this* frame; this is returned from the
65 next frame. */
66
67
68 if (REGISTER_IN_WINDOW_P(regnum))
69 {
70 frame1 = get_next_frame (frame);
71 if (!frame1) return 0; /* Registers of this frame are
72 active. */
73
74 /* Get the SP from the next frame in; it will be this
75 current frame. */
76 if (regnum != SP_REGNUM)
77 frame1 = frame;
78
79 fi = get_frame_info (frame1);
80 get_frame_saved_regs (fi, &saved_regs);
81 return saved_regs.regs[regnum]; /* ... which might be zero */
82 }
83#endif /* HAVE_REGISTER_WINDOWS */
84
85 /* Note that this next routine assumes that registers used in
86 frame x will be saved only in the frame that x calls and
87 frames interior to it. This is not true on the sparc, but the
88 above macro takes care of it, so we should be all right. */
89 while (1)
90 {
91 QUIT;
92 frame1 = get_prev_frame (frame1);
93 if (frame1 == 0 || frame1 == frame)
94 break;
95 fi = get_frame_info (frame1);
96 get_frame_saved_regs (fi, &saved_regs);
97 if (saved_regs.regs[regnum])
98 addr = saved_regs.regs[regnum];
99 }
100
101 return addr;
102}
103
104/* Find register number REGNUM relative to FRAME and put its
105 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
106 was optimized out (and thus can't be fetched). Set *LVAL to
107 lval_memory, lval_register, or not_lval, depending on whether the
108 value was fetched from memory, from a register, or in a strange
109 and non-modifiable way (e.g. a frame pointer which was calculated
110 rather than fetched). Set *ADDRP to the address, either in memory
111 on as a REGISTER_BYTE offset into the registers array.
112
113 Note that this implementation never sets *LVAL to not_lval. But
114 it can be replaced by defining GET_SAVED_REGISTER and supplying
115 your own.
116
117 The argument RAW_BUFFER must point to aligned memory. */
118void
119get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
120 char *raw_buffer;
121 int *optimized;
122 CORE_ADDR *addrp;
123 FRAME frame;
124 int regnum;
125 enum lval_type *lval;
126{
127 CORE_ADDR addr;
128 /* Normal systems don't optimize out things with register numbers. */
129 if (optimized != NULL)
130 *optimized = 0;
131 addr = find_saved_register (frame, regnum);
51b57ded 132 if (addr != 0)
bd5635a1
RP
133 {
134 if (lval != NULL)
135 *lval = lval_memory;
136 if (regnum == SP_REGNUM)
137 {
138 if (raw_buffer != NULL)
139 *(CORE_ADDR *)raw_buffer = addr;
140 if (addrp != NULL)
141 *addrp = 0;
142 return;
143 }
144 if (raw_buffer != NULL)
145 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
146 }
147 else
148 {
149 if (lval != NULL)
150 *lval = lval_register;
151 addr = REGISTER_BYTE (regnum);
152 if (raw_buffer != NULL)
153 read_register_gen (regnum, raw_buffer);
154 }
155 if (addrp != NULL)
156 *addrp = addr;
157}
158#endif /* GET_SAVED_REGISTER. */
159
160/* Copy the bytes of register REGNUM, relative to the current stack frame,
161 into our memory at MYADDR, in target byte order.
162 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
163
164 Returns 1 if could not be read, 0 if could. */
165
166int
167read_relative_register_raw_bytes (regnum, myaddr)
168 int regnum;
169 char *myaddr;
170{
171 int optim;
172 if (regnum == FP_REGNUM && selected_frame)
173 {
0791c5ea 174 memcpy (myaddr, &FRAME_FP(selected_frame), REGISTER_RAW_SIZE(FP_REGNUM));
51b57ded 175 SWAP_TARGET_AND_HOST (myaddr, REGISTER_RAW_SIZE(FP_REGNUM)); /* in target order */
bd5635a1
RP
176 return 0;
177 }
178
e1ce8aa5 179 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
bd5635a1
RP
180 regnum, (enum lval_type *)NULL);
181 return optim;
182}
183
184/* Return a `value' with the contents of register REGNUM
185 in its virtual format, with the type specified by
186 REGISTER_VIRTUAL_TYPE. */
187
188value
189value_of_register (regnum)
190 int regnum;
191{
192 CORE_ADDR addr;
193 int optim;
194 register value val;
195 char raw_buffer[MAX_REGISTER_RAW_SIZE];
196 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
197 enum lval_type lval;
198
199 get_saved_register (raw_buffer, &optim, &addr,
200 selected_frame, regnum, &lval);
201
0791c5ea 202 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
bd5635a1 203 val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
0791c5ea
JK
204 memcpy (VALUE_CONTENTS_RAW (val), virtual_buffer,
205 REGISTER_VIRTUAL_SIZE (regnum));
bd5635a1
RP
206 VALUE_LVAL (val) = lval;
207 VALUE_ADDRESS (val) = addr;
208 VALUE_REGNO (val) = regnum;
209 VALUE_OPTIMIZED_OUT (val) = optim;
210 return val;
211}
212\f
213/* Low level examining and depositing of registers.
214
215 The caller is responsible for making
216 sure that the inferior is stopped before calling the fetching routines,
217 or it will get garbage. (a change from GDB version 3, in which
218 the caller got the value from the last stop). */
219
220/* Contents of the registers in target byte order.
221 We allocate some extra slop since we do a lot of bcopy's around `registers',
222 and failing-soft is better than failing hard. */
223char registers[REGISTER_BYTES + /* SLOP */ 256];
224
225/* Nonzero if that register has been fetched. */
226char register_valid[NUM_REGS];
227
228/* Indicate that registers may have changed, so invalidate the cache. */
229void
230registers_changed ()
231{
232 int i;
233 for (i = 0; i < NUM_REGS; i++)
234 register_valid[i] = 0;
235}
236
237/* Indicate that all registers have been fetched, so mark them all valid. */
238void
239registers_fetched ()
240{
241 int i;
242 for (i = 0; i < NUM_REGS; i++)
243 register_valid[i] = 1;
244}
245
246/* Copy LEN bytes of consecutive data from registers
247 starting with the REGBYTE'th byte of register data
248 into memory at MYADDR. */
249
250void
251read_register_bytes (regbyte, myaddr, len)
252 int regbyte;
253 char *myaddr;
254 int len;
255{
256 /* Fetch all registers. */
257 int i;
258 for (i = 0; i < NUM_REGS; i++)
259 if (!register_valid[i])
260 {
261 target_fetch_registers (-1);
262 break;
263 }
264 if (myaddr != NULL)
0791c5ea 265 memcpy (myaddr, &registers[regbyte], len);
bd5635a1
RP
266}
267
268/* Read register REGNO into memory at MYADDR, which must be large enough
f2ebc25f
JK
269 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
270 If the register is known to be the size of a CORE_ADDR or smaller,
271 read_register can be used instead. */
bd5635a1
RP
272void
273read_register_gen (regno, myaddr)
274 int regno;
275 char *myaddr;
276{
277 if (!register_valid[regno])
278 target_fetch_registers (regno);
0791c5ea
JK
279 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
280 REGISTER_RAW_SIZE (regno));
bd5635a1
RP
281}
282
283/* Copy LEN bytes of consecutive data from memory at MYADDR
284 into registers starting with the REGBYTE'th byte of register data. */
285
286void
287write_register_bytes (regbyte, myaddr, len)
288 int regbyte;
289 char *myaddr;
290 int len;
291{
292 /* Make sure the entire registers array is valid. */
293 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
0791c5ea 294 memcpy (&registers[regbyte], myaddr, len);
bd5635a1
RP
295 target_store_registers (-1);
296}
297
298/* Return the contents of register REGNO, regarding it as an integer. */
51b57ded
FF
299/* FIXME, this loses when the REGISTER_VIRTUAL (REGNO) is true. Also,
300 why is the return type CORE_ADDR rather than some integer type? */
bd5635a1
RP
301
302CORE_ADDR
303read_register (regno)
304 int regno;
305{
0791c5ea 306 unsigned short sval;
df14b38b 307 unsigned int ival;
0791c5ea 308 unsigned long lval;
51b57ded 309
df14b38b
SC
310 int size;
311
bd5635a1
RP
312 if (!register_valid[regno])
313 target_fetch_registers (regno);
0791c5ea 314
df14b38b
SC
315 size = REGISTER_RAW_SIZE(regno);
316
317 if (size == sizeof (unsigned char))
318 return registers[REGISTER_BYTE (regno)];
319 else if (size == sizeof (sval))
0791c5ea 320 {
0791c5ea
JK
321 memcpy (&sval, &registers[REGISTER_BYTE (regno)], sizeof (sval));
322 SWAP_TARGET_AND_HOST (&sval, sizeof (sval));
323 return sval;
df14b38b
SC
324 }
325 else if (size == sizeof (ival))
326 {
327 memcpy (&ival, &registers[REGISTER_BYTE (regno)], sizeof (ival));
328 SWAP_TARGET_AND_HOST (&ival, sizeof (ival));
329 return ival;
330 }
331 else if (size == sizeof (lval))
332 {
0791c5ea
JK
333 memcpy (&lval, &registers[REGISTER_BYTE (regno)], sizeof (lval));
334 SWAP_TARGET_AND_HOST (&lval, sizeof (lval));
335 return lval;
df14b38b
SC
336 }
337 else
338 {
35247ccd 339 error ("GDB Internal Error in read_register() for register %d, size %d",
df14b38b 340 regno, REGISTER_RAW_SIZE(regno));
0791c5ea 341 }
bd5635a1
RP
342}
343
344/* Registers we shouldn't try to store. */
345#if !defined (CANNOT_STORE_REGISTER)
346#define CANNOT_STORE_REGISTER(regno) 0
347#endif
348
349/* Store VALUE in the register number REGNO, regarded as an integer. */
51b57ded
FF
350/* FIXME, this loses when REGISTER_VIRTUAL (REGNO) is true. Also,
351 shouldn't the val arg be a LONGEST or something? */
bd5635a1
RP
352
353void
354write_register (regno, val)
355 int regno, val;
356{
df14b38b 357 unsigned char cval;
0791c5ea 358 unsigned short sval;
df14b38b 359 unsigned int ival;
0791c5ea 360 unsigned long lval;
df14b38b
SC
361 int size;
362 PTR ptr;
363
bd5635a1
RP
364 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
365 the registers array if something writes to this register. */
366 if (CANNOT_STORE_REGISTER (regno))
367 return;
368
df14b38b
SC
369 /* If we have a valid copy of the register, and new value == old value,
370 then don't bother doing the actual store. */
bd5635a1 371
df14b38b 372 size = REGISTER_RAW_SIZE(regno);
0791c5ea 373
df14b38b 374 if (size == sizeof(cval))
0791c5ea 375 {
df14b38b
SC
376 ptr = (PTR) &cval;
377 cval = val;
378 }
379 else if (size == sizeof(sval))
380 {
381 ptr = (PTR) &sval;
0791c5ea 382 sval = val;
df14b38b
SC
383 }
384 else if (size == sizeof(ival))
385 {
386 ptr = (PTR) &ival;
387 ival = val;
388 }
389 else if (size == sizeof(lval))
390 {
391 ptr = (PTR) &lval;
0791c5ea 392 lval = val;
df14b38b
SC
393 }
394 else
395 {
35247ccd 396 error ("GDB Internal Error in write_register() for register %d, size %d",
df14b38b 397 regno, size);
0791c5ea 398 }
df14b38b
SC
399
400 if (register_valid [regno])
401 {
402 SWAP_TARGET_AND_HOST (ptr, size);
403 if (memcmp (&registers[REGISTER_BYTE (regno)],
404 ptr, size) == 0)
405 return;
406 }
407
408 target_prepare_to_store ();
409
410 memcpy (&registers[REGISTER_BYTE (regno)], ptr, size);
411
412 register_valid [regno] = 1;
bd5635a1
RP
413
414 target_store_registers (regno);
415}
416
417/* Record that register REGNO contains VAL.
418 This is used when the value is obtained from the inferior or core dump,
419 so there is no need to store the value there. */
420
421void
422supply_register (regno, val)
423 int regno;
424 char *val;
425{
426 register_valid[regno] = 1;
0791c5ea
JK
427 memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
428
429 /* On some architectures, e.g. HPPA, there are a few stray bits in some
430 registers, that the rest of the code would like to ignore. */
431#ifdef CLEAN_UP_REGISTER_VALUE
432 CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
433#endif
bd5635a1
RP
434}
435\f
436/* Given a struct symbol for a variable,
437 and a stack frame id, read the value of the variable
438 and return a (pointer to a) struct value containing the value.
777bef06
JK
439 If the variable cannot be found, return a zero pointer.
440 If FRAME is NULL, use the selected_frame. */
bd5635a1
RP
441
442value
443read_var_value (var, frame)
444 register struct symbol *var;
445 FRAME frame;
446{
447 register value v;
448 struct frame_info *fi;
449 struct type *type = SYMBOL_TYPE (var);
450 CORE_ADDR addr;
bd5635a1
RP
451 register int len;
452
453 v = allocate_value (type);
454 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
455 len = TYPE_LENGTH (type);
456
457 if (frame == 0) frame = selected_frame;
458
459 switch (SYMBOL_CLASS (var))
460 {
461 case LOC_CONST:
0791c5ea 462 memcpy (VALUE_CONTENTS_RAW (v), &SYMBOL_VALUE (var), len);
bd5635a1
RP
463 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
464 VALUE_LVAL (v) = not_lval;
465 return v;
466
467 case LOC_LABEL:
468 addr = SYMBOL_VALUE_ADDRESS (var);
0791c5ea 469 memcpy (VALUE_CONTENTS_RAW (v), &addr, len);
bd5635a1
RP
470 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
471 VALUE_LVAL (v) = not_lval;
472 return v;
473
474 case LOC_CONST_BYTES:
36b9d39c
JG
475 {
476 char *bytes_addr;
477 bytes_addr = SYMBOL_VALUE_BYTES (var);
0791c5ea 478 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
36b9d39c
JG
479 VALUE_LVAL (v) = not_lval;
480 return v;
481 }
bd5635a1
RP
482
483 case LOC_STATIC:
bd5635a1
RP
484 addr = SYMBOL_VALUE_ADDRESS (var);
485 break;
486
bd5635a1 487 case LOC_ARG:
51b57ded
FF
488 if (SYMBOL_BASEREG_VALID (var))
489 {
490 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
491 }
492 else
493 {
494 fi = get_frame_info (frame);
495 if (fi == NULL)
496 return 0;
497 addr = FRAME_ARGS_ADDRESS (fi);
498 }
499 if (!addr)
500 {
501 return 0;
502 }
bd5635a1
RP
503 addr += SYMBOL_VALUE (var);
504 break;
505
506 case LOC_REF_ARG:
51b57ded
FF
507 if (SYMBOL_BASEREG_VALID (var))
508 {
509 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
510 }
511 else
512 {
513 fi = get_frame_info (frame);
514 if (fi == NULL)
515 return 0;
516 addr = FRAME_ARGS_ADDRESS (fi);
517 }
518 if (!addr)
519 {
520 return 0;
521 }
bd5635a1 522 addr += SYMBOL_VALUE (var);
51b57ded 523 read_memory (addr, (char *) &addr, sizeof (CORE_ADDR));
bd5635a1
RP
524 break;
525
526 case LOC_LOCAL:
527 case LOC_LOCAL_ARG:
51b57ded
FF
528 if (SYMBOL_BASEREG_VALID (var))
529 {
530 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
531 }
532 else
533 {
534 fi = get_frame_info (frame);
535 if (fi == NULL)
536 return 0;
537 addr = FRAME_LOCALS_ADDRESS (fi);
538 }
539 addr += SYMBOL_VALUE (var);
bd5635a1
RP
540 break;
541
542 case LOC_TYPEDEF:
543 error ("Cannot look up value of a typedef");
544 break;
545
546 case LOC_BLOCK:
547 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
548 return v;
549
550 case LOC_REGISTER:
551 case LOC_REGPARM:
35247ccd 552 case LOC_REGPARM_ADDR:
bd5635a1 553 {
777bef06 554 struct block *b;
bd5635a1 555
777bef06
JK
556 if (frame == NULL)
557 return 0;
558 b = get_frame_block (frame);
559
bd5635a1
RP
560 v = value_from_register (type, SYMBOL_VALUE (var), frame);
561
35247ccd 562 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
0791c5ea
JK
563 {
564 addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
565 VALUE_LVAL (v) = lval_memory;
566 }
bd5635a1
RP
567 else
568 return v;
569 }
570 break;
571
35247ccd
SG
572 case LOC_OPTIMIZED_OUT:
573 VALUE_LVAL (v) = not_lval;
574 VALUE_OPTIMIZED_OUT (v) = 1;
575 return v;
576
bd5635a1
RP
577 default:
578 error ("Cannot look up value of a botched symbol.");
579 break;
580 }
581
582 VALUE_ADDRESS (v) = addr;
583 VALUE_LAZY (v) = 1;
584 return v;
585}
586
587/* Return a value of type TYPE, stored in register REGNUM, in frame
588 FRAME. */
589
590value
591value_from_register (type, regnum, frame)
592 struct type *type;
593 int regnum;
594 FRAME frame;
595{
596 char raw_buffer [MAX_REGISTER_RAW_SIZE];
597 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
598 CORE_ADDR addr;
599 int optim;
600 value v = allocate_value (type);
601 int len = TYPE_LENGTH (type);
602 char *value_bytes = 0;
603 int value_bytes_copied = 0;
604 int num_storage_locs;
605 enum lval_type lval;
606
607 VALUE_REGNO (v) = regnum;
608
609 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
610 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
611 1);
612
0791c5ea
JK
613 if (num_storage_locs > 1
614#ifdef GDB_TARGET_IS_H8500
615 || TYPE_CODE (type) == TYPE_CODE_PTR
616#endif
617 )
bd5635a1
RP
618 {
619 /* Value spread across multiple storage locations. */
620
621 int local_regnum;
622 int mem_stor = 0, reg_stor = 0;
623 int mem_tracking = 1;
624 CORE_ADDR last_addr = 0;
625 CORE_ADDR first_addr;
626
627 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
628
629 /* Copy all of the data out, whereever it may be. */
630
0791c5ea
JK
631#ifdef GDB_TARGET_IS_H8500
632/* This piece of hideosity is required because the H8500 treats registers
633 differently depending upon whether they are used as pointers or not. As a
634 pointer, a register needs to have a page register tacked onto the front.
635 An alternate way to do this would be to have gcc output different register
636 numbers for the pointer & non-pointer form of the register. But, it
637 doesn't, so we're stuck with this. */
638
35247ccd
SG
639 if (TYPE_CODE (type) == TYPE_CODE_PTR
640 && len > 2)
bd5635a1 641 {
0791c5ea
JK
642 int page_regnum;
643
644 switch (regnum)
645 {
646 case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
647 page_regnum = SEG_D_REGNUM;
648 break;
649 case R4_REGNUM: case R5_REGNUM:
650 page_regnum = SEG_E_REGNUM;
651 break;
652 case R6_REGNUM: case R7_REGNUM:
653 page_regnum = SEG_T_REGNUM;
654 break;
655 }
656
657 value_bytes[0] = 0;
658 get_saved_register (value_bytes + 1,
bd5635a1
RP
659 &optim,
660 &addr,
661 frame,
0791c5ea 662 page_regnum,
bd5635a1 663 &lval);
0791c5ea 664
bd5635a1
RP
665 if (lval == lval_register)
666 reg_stor++;
667 else
df14b38b
SC
668 mem_stor++;
669 first_addr = addr;
0791c5ea 670 last_addr = addr;
bd5635a1 671
0791c5ea
JK
672 get_saved_register (value_bytes + 2,
673 &optim,
674 &addr,
675 frame,
676 regnum,
677 &lval);
678
679 if (lval == lval_register)
680 reg_stor++;
681 else
682 {
683 mem_stor++;
684 mem_tracking = mem_tracking && (addr == last_addr);
bd5635a1
RP
685 }
686 last_addr = addr;
687 }
0791c5ea
JK
688 else
689#endif /* GDB_TARGET_IS_H8500 */
690 for (local_regnum = regnum;
691 value_bytes_copied < len;
692 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
693 ++local_regnum))
694 {
695 get_saved_register (value_bytes + value_bytes_copied,
696 &optim,
697 &addr,
698 frame,
699 local_regnum,
700 &lval);
df14b38b
SC
701
702 if (regnum == local_regnum)
703 first_addr = addr;
0791c5ea
JK
704 if (lval == lval_register)
705 reg_stor++;
706 else
707 {
708 mem_stor++;
0791c5ea
JK
709
710 mem_tracking =
711 (mem_tracking
712 && (regnum == local_regnum
713 || addr == last_addr));
714 }
715 last_addr = addr;
716 }
bd5635a1
RP
717
718 if ((reg_stor && mem_stor)
719 || (mem_stor && !mem_tracking))
720 /* Mixed storage; all of the hassle we just went through was
721 for some good purpose. */
722 {
723 VALUE_LVAL (v) = lval_reg_frame_relative;
724 VALUE_FRAME (v) = FRAME_FP (frame);
725 VALUE_FRAME_REGNUM (v) = regnum;
726 }
727 else if (mem_stor)
728 {
729 VALUE_LVAL (v) = lval_memory;
730 VALUE_ADDRESS (v) = first_addr;
731 }
732 else if (reg_stor)
733 {
734 VALUE_LVAL (v) = lval_register;
735 VALUE_ADDRESS (v) = first_addr;
736 }
737 else
738 fatal ("value_from_register: Value not stored anywhere!");
739
740 VALUE_OPTIMIZED_OUT (v) = optim;
741
742 /* Any structure stored in more than one register will always be
743 an integral number of registers. Otherwise, you'd need to do
744 some fiddling with the last register copied here for little
745 endian machines. */
746
747 /* Copy into the contents section of the value. */
0791c5ea 748 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
bd5635a1 749
df14b38b
SC
750 /* Finally do any conversion necessary when extracting this
751 type from more than one register. */
752#ifdef REGISTER_CONVERT_TO_TYPE
753 REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
754#endif
bd5635a1
RP
755 return v;
756 }
757
758 /* Data is completely contained within a single register. Locate the
759 register's contents in a real register or in core;
760 read the data in raw format. */
761
762 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
763 VALUE_OPTIMIZED_OUT (v) = optim;
764 VALUE_LVAL (v) = lval;
765 VALUE_ADDRESS (v) = addr;
766
767 /* Convert the raw contents to virtual contents.
768 (Just copy them if the formats are the same.) */
769
0791c5ea 770 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
bd5635a1
RP
771
772 if (REGISTER_CONVERTIBLE (regnum))
773 {
774 /* When the raw and virtual formats differ, the virtual format
775 corresponds to a specific data type. If we want that type,
776 copy the data into the value.
777 Otherwise, do a type-conversion. */
778
779 if (type != REGISTER_VIRTUAL_TYPE (regnum))
780 {
781 /* eg a variable of type `float' in a 68881 register
782 with raw type `extended' and virtual type `double'.
783 Fetch it as a `double' and then convert to `float'. */
784 v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
0791c5ea 785 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
bd5635a1
RP
786 v = value_cast (type, v);
787 }
788 else
0791c5ea 789 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
bd5635a1
RP
790 }
791 else
792 {
793 /* Raw and virtual formats are the same for this register. */
794
795#if TARGET_BYTE_ORDER == BIG_ENDIAN
796 if (len < REGISTER_RAW_SIZE (regnum))
797 {
798 /* Big-endian, and we want less than full size. */
799 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
800 }
801#endif
802
0791c5ea 803 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer + VALUE_OFFSET (v), len);
bd5635a1
RP
804 }
805
806 return v;
807}
808\f
36b9d39c 809/* Given a struct symbol for a variable or function,
bd5635a1 810 and a stack frame id,
36b9d39c
JG
811 return a (pointer to a) struct value containing the properly typed
812 address. */
bd5635a1
RP
813
814value
815locate_var_value (var, frame)
816 register struct symbol *var;
817 FRAME frame;
818{
819 CORE_ADDR addr = 0;
820 struct type *type = SYMBOL_TYPE (var);
bd5635a1
RP
821 value lazy_value;
822
823 /* Evaluate it first; if the result is a memory address, we're fine.
824 Lazy evaluation pays off here. */
825
826 lazy_value = read_var_value (var, frame);
827 if (lazy_value == 0)
0791c5ea 828 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
bd5635a1 829
36b9d39c
JG
830 if (VALUE_LAZY (lazy_value)
831 || TYPE_CODE (type) == TYPE_CODE_FUNC)
bd5635a1
RP
832 {
833 addr = VALUE_ADDRESS (lazy_value);
7d9884b9 834 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
bd5635a1
RP
835 }
836
837 /* Not a memory address; check what the problem was. */
838 switch (VALUE_LVAL (lazy_value))
839 {
840 case lval_register:
841 case lval_reg_frame_relative:
842 error ("Address requested for identifier \"%s\" which is in a register.",
0791c5ea 843 SYMBOL_SOURCE_NAME (var));
bd5635a1
RP
844 break;
845
846 default:
847 error ("Can't take address of \"%s\" which isn't an lvalue.",
0791c5ea 848 SYMBOL_SOURCE_NAME (var));
bd5635a1
RP
849 break;
850 }
851 return 0; /* For lint -- never reached */
852}
This page took 0.126612 seconds and 4 git commands to generate.