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