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