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