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