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