findvar.c revision 46283
1/* Find a variable's value in memory, for GDB, the GNU debugger.
2   Copyright 1986, 87, 89, 91, 94, 95, 96, 1998
3   Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21#include "defs.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "frame.h"
25#include "value.h"
26#include "gdbcore.h"
27#include "inferior.h"
28#include "target.h"
29#include "gdb_string.h"
30#include "floatformat.h"
31#include "symfile.h"	/* for overlay functions */
32
33/* This is used to indicate that we don't know the format of the floating point
34   number.  Typically, this is useful for native ports, where the actual format
35   is irrelevant, since no conversions will be taking place.  */
36
37const struct floatformat floatformat_unknown;
38
39/* Registers we shouldn't try to store.  */
40#if !defined (CANNOT_STORE_REGISTER)
41#define CANNOT_STORE_REGISTER(regno) 0
42#endif
43
44static void write_register_gen PARAMS ((int, char *));
45
46/* Basic byte-swapping routines.  GDB has needed these for a long time...
47   All extract a target-format integer at ADDR which is LEN bytes long.  */
48
49#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
50  /* 8 bit characters are a pretty safe assumption these days, so we
51     assume it throughout all these swapping routines.  If we had to deal with
52     9 bit characters, we would need to make len be in bits and would have
53     to re-write these routines...  */
54  you lose
55#endif
56
57LONGEST
58extract_signed_integer (addr, len)
59     PTR addr;
60     int len;
61{
62  LONGEST retval;
63  unsigned char *p;
64  unsigned char *startaddr = (unsigned char *)addr;
65  unsigned char *endaddr = startaddr + len;
66
67  if (len > (int) sizeof (LONGEST))
68    error ("\
69That operation is not available on integers of more than %d bytes.",
70	   sizeof (LONGEST));
71
72  /* Start at the most significant end of the integer, and work towards
73     the least significant.  */
74  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
75    {
76      p = startaddr;
77      /* Do the sign extension once at the start.  */
78      retval = ((LONGEST)*p ^ 0x80) - 0x80;
79      for (++p; p < endaddr; ++p)
80	retval = (retval << 8) | *p;
81    }
82  else
83    {
84      p = endaddr - 1;
85      /* Do the sign extension once at the start.  */
86      retval = ((LONGEST)*p ^ 0x80) - 0x80;
87      for (--p; p >= startaddr; --p)
88	retval = (retval << 8) | *p;
89    }
90  return retval;
91}
92
93ULONGEST
94extract_unsigned_integer (addr, len)
95     PTR addr;
96     int len;
97{
98  ULONGEST retval;
99  unsigned char *p;
100  unsigned char *startaddr = (unsigned char *)addr;
101  unsigned char *endaddr = startaddr + len;
102
103  if (len > (int) sizeof (ULONGEST))
104    error ("\
105That operation is not available on integers of more than %d bytes.",
106	   sizeof (ULONGEST));
107
108  /* Start at the most significant end of the integer, and work towards
109     the least significant.  */
110  retval = 0;
111  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
112    {
113      for (p = startaddr; p < endaddr; ++p)
114	retval = (retval << 8) | *p;
115    }
116  else
117    {
118      for (p = endaddr - 1; p >= startaddr; --p)
119	retval = (retval << 8) | *p;
120    }
121  return retval;
122}
123
124/* Sometimes a long long unsigned integer can be extracted as a
125   LONGEST value.  This is done so that we can print these values
126   better.  If this integer can be converted to a LONGEST, this
127   function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
128
129int
130extract_long_unsigned_integer (addr, orig_len, pval)
131     PTR addr;
132     int orig_len;
133     LONGEST *pval;
134{
135  char *p, *first_addr;
136  int len;
137
138  len = orig_len;
139  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
140    {
141      for (p = (char *) addr;
142	   len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
143	   p++)
144	{
145	  if (*p == 0)
146	    len--;
147	  else
148	    break;
149	}
150      first_addr = p;
151    }
152  else
153    {
154      first_addr = (char *) addr;
155      for (p = (char *) addr + orig_len - 1;
156	   len > (int) sizeof (LONGEST) && p >= (char *) addr;
157	   p--)
158	{
159	  if (*p == 0)
160	    len--;
161	  else
162	    break;
163	}
164    }
165
166  if (len <= (int) sizeof (LONGEST))
167    {
168      *pval = (LONGEST) extract_unsigned_integer (first_addr,
169						  sizeof (LONGEST));
170      return 1;
171    }
172
173  return 0;
174}
175
176CORE_ADDR
177extract_address (addr, len)
178     PTR addr;
179     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
186void
187store_signed_integer (addr, len, val)
188     PTR addr;
189     int len;
190     LONGEST val;
191{
192  unsigned char *p;
193  unsigned char *startaddr = (unsigned char *)addr;
194  unsigned char *endaddr = startaddr + len;
195
196  /* Start at the least significant end of the integer, and work towards
197     the most significant.  */
198  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
199    {
200      for (p = endaddr - 1; p >= startaddr; --p)
201	{
202	  *p = val & 0xff;
203	  val >>= 8;
204	}
205    }
206  else
207    {
208      for (p = startaddr; p < endaddr; ++p)
209	{
210	  *p = val & 0xff;
211	  val >>= 8;
212	}
213    }
214}
215
216void
217store_unsigned_integer (addr, len, val)
218     PTR addr;
219     int len;
220     ULONGEST val;
221{
222  unsigned char *p;
223  unsigned char *startaddr = (unsigned char *)addr;
224  unsigned char *endaddr = startaddr + len;
225
226  /* Start at the least significant end of the integer, and work towards
227     the most significant.  */
228  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
229    {
230      for (p = endaddr - 1; p >= startaddr; --p)
231	{
232	  *p = val & 0xff;
233	  val >>= 8;
234	}
235    }
236  else
237    {
238      for (p = startaddr; p < endaddr; ++p)
239	{
240	  *p = val & 0xff;
241	  val >>= 8;
242	}
243    }
244}
245
246/* Store the literal address "val" into
247   gdb-local memory pointed to by "addr"
248   for "len" bytes. */
249void
250store_address (addr, len, val)
251     PTR addr;
252     int len;
253     LONGEST val;
254{
255  if( TARGET_BYTE_ORDER == BIG_ENDIAN
256      &&  len != sizeof( LONGEST )) {
257    /* On big-endian machines (e.g., HPPA 2.0, narrow mode)
258     * just letting this fall through to the call below will
259     * lead to the wrong bits being stored.
260     *
261     * Only the simplest case is fixed here, the others just
262     * get the old behavior.
263     */
264    if( (len == sizeof( CORE_ADDR ))
265	&&  (sizeof( LONGEST ) == 2 * sizeof( CORE_ADDR ))) {
266      /* Watch out!  The high bits are garbage! */
267      CORE_ADDR coerce[2];
268      *(LONGEST*)&coerce = val;
269
270      store_unsigned_integer (addr, len, coerce[1] ); /* BIG_ENDIAN code! */
271      return;
272    }
273  }
274  store_unsigned_integer (addr, len, val);
275}
276
277/* Swap LEN bytes at BUFFER between target and host byte-order.  */
278#define SWAP_FLOATING(buffer,len) \
279  do                                                                    \
280    {                                                                   \
281      if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER)                         \
282        {                                                               \
283          char tmp;                                                     \
284          char *p = (char *)(buffer);                                   \
285          char *q = ((char *)(buffer)) + len - 1;                       \
286          for (; p < q; p++, q--)                                       \
287            {                                                           \
288              tmp = *q;                                                 \
289              *q = *p;                                                  \
290              *p = tmp;                                                 \
291            }                                                           \
292        }                                                               \
293    }                                                                   \
294  while (0)
295
296/* Extract a floating-point number from a target-order byte-stream at ADDR.
297   Returns the value as type DOUBLEST.
298
299   If the host and target formats agree, we just copy the raw data into the
300   appropriate type of variable and return, letting the host increase precision
301   as necessary.  Otherwise, we call the conversion routine and let it do the
302   dirty work.  */
303
304DOUBLEST
305extract_floating (addr, len)
306     PTR addr;
307     int len;
308{
309  DOUBLEST dretval;
310
311  if (len == sizeof (float))
312    {
313      if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
314	{
315	  float retval;
316
317	  memcpy (&retval, addr, sizeof (retval));
318	  return retval;
319	}
320      else
321	floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
322    }
323  else if (len == sizeof (double))
324    {
325      if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
326	{
327	  double retval;
328
329	  memcpy (&retval, addr, sizeof (retval));
330	  return retval;
331	}
332      else
333	floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
334    }
335  else if (len == sizeof (DOUBLEST))
336    {
337      if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
338	{
339	  DOUBLEST retval;
340
341	  memcpy (&retval, addr, sizeof (retval));
342	  return retval;
343	}
344      else
345	floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
346    }
347  else
348    {
349      error ("Can't deal with a floating point number of %d bytes.", len);
350    }
351
352  return dretval;
353}
354
355void
356store_floating (addr, len, val)
357     PTR addr;
358     int len;
359     DOUBLEST val;
360{
361  if (len == sizeof (float))
362    {
363      if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
364	{
365	  float floatval = val;
366
367	  memcpy (addr, &floatval, sizeof (floatval));
368	}
369      else
370	floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
371    }
372  else if (len == sizeof (double))
373    {
374      if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
375	{
376	  double doubleval = val;
377
378	  memcpy (addr, &doubleval, sizeof (doubleval));
379	}
380      else
381	floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
382    }
383  else if (len == sizeof (DOUBLEST))
384    {
385      if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
386	memcpy (addr, &val, sizeof (val));
387      else
388	floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
389    }
390  else
391    {
392      error ("Can't deal with a floating point number of %d bytes.", len);
393    }
394}
395
396#if !defined (GET_SAVED_REGISTER)
397
398/* Return the address in which frame FRAME's value of register REGNUM
399   has been saved in memory.  Or return zero if it has not been saved.
400   If REGNUM specifies the SP, the value we return is actually
401   the SP value, not an address where it was saved.  */
402
403CORE_ADDR
404find_saved_register (frame, regnum)
405     struct frame_info *frame;
406     int regnum;
407{
408  register struct frame_info *frame1 = NULL;
409  register CORE_ADDR addr = 0;
410
411  if (frame == NULL)		/* No regs saved if want current frame */
412    return 0;
413
414#ifdef HAVE_REGISTER_WINDOWS
415  /* We assume that a register in a register window will only be saved
416     in one place (since the name changes and/or disappears as you go
417     towards inner frames), so we only call get_frame_saved_regs on
418     the current frame.  This is directly in contradiction to the
419     usage below, which assumes that registers used in a frame must be
420     saved in a lower (more interior) frame.  This change is a result
421     of working on a register window machine; get_frame_saved_regs
422     always returns the registers saved within a frame, within the
423     context (register namespace) of that frame. */
424
425  /* However, note that we don't want this to return anything if
426     nothing is saved (if there's a frame inside of this one).  Also,
427     callers to this routine asking for the stack pointer want the
428     stack pointer saved for *this* frame; this is returned from the
429     next frame.  */
430
431  if (REGISTER_IN_WINDOW_P(regnum))
432    {
433      frame1 = get_next_frame (frame);
434      if (!frame1) return 0;	/* Registers of this frame are active.  */
435
436      /* Get the SP from the next frame in; it will be this
437	 current frame.  */
438      if (regnum != SP_REGNUM)
439	frame1 = frame;
440
441      FRAME_INIT_SAVED_REGS (frame1);
442      return frame1->saved_regs[regnum];	/* ... which might be zero */
443    }
444#endif /* HAVE_REGISTER_WINDOWS */
445
446  /* Note that this next routine assumes that registers used in
447     frame x will be saved only in the frame that x calls and
448     frames interior to it.  This is not true on the sparc, but the
449     above macro takes care of it, so we should be all right. */
450  while (1)
451    {
452      QUIT;
453      frame1 = get_prev_frame (frame1);
454      if (frame1 == 0 || frame1 == frame)
455	break;
456      FRAME_INIT_SAVED_REGS (frame1);
457      if (frame1->saved_regs[regnum])
458	addr = frame1->saved_regs[regnum];
459    }
460
461  return addr;
462}
463
464/* Find register number REGNUM relative to FRAME and put its (raw,
465   target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
466   variable was optimized out (and thus can't be fetched).  Set *LVAL
467   to lval_memory, lval_register, or not_lval, depending on whether
468   the value was fetched from memory, from a register, or in a strange
469   and non-modifiable way (e.g. a frame pointer which was calculated
470   rather than fetched).  Set *ADDRP to the address, either in memory
471   on as a REGISTER_BYTE offset into the registers array.
472
473   Note that this implementation never sets *LVAL to not_lval.  But
474   it can be replaced by defining GET_SAVED_REGISTER and supplying
475   your own.
476
477   The argument RAW_BUFFER must point to aligned memory.  */
478
479void
480get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
481     char *raw_buffer;
482     int *optimized;
483     CORE_ADDR *addrp;
484     struct frame_info *frame;
485     int regnum;
486     enum lval_type *lval;
487{
488  CORE_ADDR addr;
489
490  if (!target_has_registers)
491    error ("No registers.");
492
493  /* Normal systems don't optimize out things with register numbers.  */
494  if (optimized != NULL)
495    *optimized = 0;
496  addr = find_saved_register (frame, regnum);
497  if (addr != 0)
498    {
499      if (lval != NULL)
500	*lval = lval_memory;
501      if (regnum == SP_REGNUM)
502	{
503	  if (raw_buffer != NULL)
504	    {
505	      /* Put it back in target format.  */
506	      store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), (LONGEST)addr);
507	    }
508	  if (addrp != NULL)
509	    *addrp = 0;
510	  return;
511	}
512      if (raw_buffer != NULL)
513	read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
514    }
515  else
516    {
517      if (lval != NULL)
518	*lval = lval_register;
519      addr = REGISTER_BYTE (regnum);
520      if (raw_buffer != NULL)
521	read_register_gen (regnum, raw_buffer);
522    }
523  if (addrp != NULL)
524    *addrp = addr;
525}
526#endif /* GET_SAVED_REGISTER.  */
527
528/* Copy the bytes of register REGNUM, relative to the input stack frame,
529   into our memory at MYADDR, in target byte order.
530   The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
531
532   Returns 1 if could not be read, 0 if could.  */
533
534int
535read_relative_register_raw_bytes_for_frame (regnum, myaddr, frame)
536     int regnum;
537     char *myaddr;
538     struct frame_info *frame;
539{
540  int optim;
541  if (regnum == FP_REGNUM && frame)
542    {
543      /* Put it back in target format. */
544      store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
545		     (LONGEST)FRAME_FP(frame));
546
547      return 0;
548    }
549
550  get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
551                      regnum, (enum lval_type *)NULL);
552
553  if (register_valid [regnum] < 0)
554    return 1;	/* register value not available */
555
556  return optim;
557}
558
559/* Copy the bytes of register REGNUM, relative to the current stack frame,
560   into our memory at MYADDR, in target byte order.
561   The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
562
563   Returns 1 if could not be read, 0 if could.  */
564
565int
566read_relative_register_raw_bytes (regnum, myaddr)
567     int regnum;
568     char *myaddr;
569{
570  return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
571						     selected_frame);
572}
573
574/* Return a `value' with the contents of register REGNUM
575   in its virtual format, with the type specified by
576   REGISTER_VIRTUAL_TYPE.
577
578   NOTE: returns NULL if register value is not available.
579   Caller will check return value or die!  */
580
581value_ptr
582value_of_register (regnum)
583     int regnum;
584{
585  CORE_ADDR addr;
586  int optim;
587  register value_ptr reg_val;
588  char raw_buffer[MAX_REGISTER_RAW_SIZE];
589  enum lval_type lval;
590
591  get_saved_register (raw_buffer, &optim, &addr,
592		      selected_frame, regnum, &lval);
593
594  if (register_valid[regnum] < 0)
595    return NULL;	/* register value not available */
596
597  reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
598
599  /* Convert raw data to virtual format if necessary.  */
600
601#ifdef REGISTER_CONVERTIBLE
602  if (REGISTER_CONVERTIBLE (regnum))
603    {
604      REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
605				   raw_buffer, VALUE_CONTENTS_RAW (reg_val));
606    }
607  else
608#endif
609    if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
610      memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
611	      REGISTER_RAW_SIZE (regnum));
612    else
613      fatal ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
614	     REGISTER_NAME (regnum), regnum,
615	     REGISTER_RAW_SIZE (regnum), REGISTER_VIRTUAL_SIZE (regnum));
616  VALUE_LVAL (reg_val) = lval;
617  VALUE_ADDRESS (reg_val) = addr;
618  VALUE_REGNO (reg_val) = regnum;
619  VALUE_OPTIMIZED_OUT (reg_val) = optim;
620  return reg_val;
621}
622
623/* Low level examining and depositing of registers.
624
625   The caller is responsible for making
626   sure that the inferior is stopped before calling the fetching routines,
627   or it will get garbage.  (a change from GDB version 3, in which
628   the caller got the value from the last stop).  */
629
630/* Contents of the registers in target byte order.
631   We allocate some extra slop since we do a lot of memcpy's around
632   `registers', and failing-soft is better than failing hard.  */
633
634char registers[REGISTER_BYTES + /* SLOP */ 256];
635
636/* Nonzero if that register has been fetched,
637   -1 if register value not available. */
638SIGNED char register_valid[NUM_REGS];
639
640/* The thread/process associated with the current set of registers.  For now,
641   -1 is special, and means `no current process'.  */
642int registers_pid = -1;
643
644/* Indicate that registers may have changed, so invalidate the cache.  */
645
646void
647registers_changed ()
648{
649  int i;
650  int numregs = ARCH_NUM_REGS;
651
652  registers_pid = -1;
653
654  /* Force cleanup of any alloca areas if using C alloca instead of
655     a builtin alloca.  This particular call is used to clean up
656     areas allocated by low level target code which may build up
657     during lengthy interactions between gdb and the target before
658     gdb gives control to the user (ie watchpoints).  */
659  alloca (0);
660
661  for (i = 0; i < numregs; i++)
662    register_valid[i] = 0;
663
664  if (registers_changed_hook)
665    registers_changed_hook ();
666}
667
668/* Indicate that all registers have been fetched, so mark them all valid.  */
669void
670registers_fetched ()
671{
672  int i;
673  int numregs = ARCH_NUM_REGS;
674  for (i = 0; i < numregs; i++)
675    register_valid[i] = 1;
676}
677
678/* read_register_bytes and write_register_bytes are generally a *BAD* idea.
679   They are inefficient because they need to check for partial updates, which
680   can only be done by scanning through all of the registers and seeing if the
681   bytes that are being read/written fall inside of an invalid register.  [The
682    main reason this is necessary is that register sizes can vary, so a simple
683    index won't suffice.]  It is far better to call read_register_gen if you
684   want to get at the raw register contents, as it only takes a regno as an
685   argument, and therefore can't do a partial register update.  It would also
686   be good to have a write_register_gen for similar reasons.
687
688   Prior to the recent fixes to check for partial updates, both read and
689   write_register_bytes always checked to see if any registers were stale, and
690   then called target_fetch_registers (-1) to update the whole set.  This
691   caused really slowed things down for remote targets.  */
692
693/* Copy INLEN bytes of consecutive data from registers
694   starting with the INREGBYTE'th byte of register data
695   into memory at MYADDR.  */
696
697void
698read_register_bytes (inregbyte, myaddr, inlen)
699     int inregbyte;
700     char *myaddr;
701     int inlen;
702{
703  int inregend = inregbyte + inlen;
704  int regno;
705
706  if (registers_pid != inferior_pid)
707    {
708      registers_changed ();
709      registers_pid = inferior_pid;
710    }
711
712  /* See if we are trying to read bytes from out-of-date registers.  If so,
713     update just those registers.  */
714
715  for (regno = 0; regno < NUM_REGS; regno++)
716    {
717      int regstart, regend;
718      int startin, endin;
719
720      if (register_valid[regno])
721	continue;
722
723      if (REGISTER_NAME (regno) == NULL || *REGISTER_NAME (regno) == '\0')
724	continue;
725
726      regstart = REGISTER_BYTE (regno);
727      regend = regstart + REGISTER_RAW_SIZE (regno);
728
729      startin = regstart >= inregbyte && regstart < inregend;
730      endin = regend > inregbyte && regend <= inregend;
731
732      if (!startin && !endin)
733	continue;
734
735      /* We've found an invalid register where at least one byte will be read.
736	 Update it from the target.  */
737
738      target_fetch_registers (regno);
739
740      if (!register_valid[regno])
741	error ("read_register_bytes:  Couldn't update register %d.", regno);
742    }
743
744  if (myaddr != NULL)
745    memcpy (myaddr, &registers[inregbyte], inlen);
746}
747
748/* Read register REGNO into memory at MYADDR, which must be large enough
749   for REGISTER_RAW_BYTES (REGNO).  Target byte-order.
750   If the register is known to be the size of a CORE_ADDR or smaller,
751   read_register can be used instead.  */
752void
753read_register_gen (regno, myaddr)
754     int regno;
755     char *myaddr;
756{
757  if (registers_pid != inferior_pid)
758    {
759      registers_changed ();
760      registers_pid = inferior_pid;
761    }
762
763  if (!register_valid[regno])
764    target_fetch_registers (regno);
765  memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
766	  REGISTER_RAW_SIZE (regno));
767}
768
769/* Write register REGNO at MYADDR to the target.  MYADDR points at
770   REGISTER_RAW_BYTES(REGNO), which must be in target byte-order.  */
771
772static void
773write_register_gen (regno, myaddr)
774     int regno;
775     char *myaddr;
776{
777  int size;
778
779  /* On the sparc, writing %g0 is a no-op, so we don't even want to change
780     the registers array if something writes to this register.  */
781  if (CANNOT_STORE_REGISTER (regno))
782    return;
783
784  if (registers_pid != inferior_pid)
785    {
786      registers_changed ();
787      registers_pid = inferior_pid;
788    }
789
790  size = REGISTER_RAW_SIZE(regno);
791
792  /* If we have a valid copy of the register, and new value == old value,
793     then don't bother doing the actual store. */
794
795  if (register_valid [regno]
796      && memcmp (&registers[REGISTER_BYTE (regno)], myaddr, size) == 0)
797    return;
798
799  target_prepare_to_store ();
800
801  memcpy (&registers[REGISTER_BYTE (regno)], myaddr, size);
802
803  register_valid [regno] = 1;
804
805  target_store_registers (regno);
806}
807
808/* Copy INLEN bytes of consecutive data from memory at MYADDR
809   into registers starting with the MYREGSTART'th byte of register data.  */
810
811void
812write_register_bytes (myregstart, myaddr, inlen)
813     int myregstart;
814     char *myaddr;
815     int inlen;
816{
817  int myregend = myregstart + inlen;
818  int regno;
819
820  target_prepare_to_store ();
821
822  /* Scan through the registers updating any that are covered by the range
823     myregstart<=>myregend using write_register_gen, which does nice things
824     like handling threads, and avoiding updates when the new and old contents
825     are the same.  */
826
827  for (regno = 0; regno < NUM_REGS; regno++)
828    {
829      int regstart, regend;
830      int startin, endin;
831      char regbuf[MAX_REGISTER_RAW_SIZE];
832
833      regstart = REGISTER_BYTE (regno);
834      regend = regstart + REGISTER_RAW_SIZE (regno);
835
836      startin = regstart >= myregstart && regstart < myregend;
837      endin = regend > myregstart && regend <= myregend;
838
839      if (!startin && !endin)
840	continue;		/* Register is completely out of range */
841
842      if (startin && endin)	/* register is completely in range */
843	{
844	  write_register_gen (regno, myaddr + (regstart - myregstart));
845	  continue;
846	}
847
848      /* We may be doing a partial update of an invalid register.  Update it
849	 from the target before scribbling on it.  */
850      read_register_gen (regno, regbuf);
851
852      if (startin)
853	memcpy (registers + regstart,
854		myaddr + regstart - myregstart,
855		myregend - regstart);
856      else			/* endin */
857	memcpy (registers + myregstart,
858		myaddr,
859		regend - myregstart);
860      target_store_registers (regno);
861    }
862}
863
864/* Return the raw contents of register REGNO, regarding it as an integer.  */
865/* This probably should be returning LONGEST rather than CORE_ADDR.  */
866
867CORE_ADDR
868read_register (regno)
869     int regno;
870{
871  if (registers_pid != inferior_pid)
872    {
873      registers_changed ();
874      registers_pid = inferior_pid;
875    }
876
877  if (!register_valid[regno])
878    target_fetch_registers (regno);
879
880  return (CORE_ADDR)extract_address (&registers[REGISTER_BYTE (regno)],
881				     REGISTER_RAW_SIZE(regno));
882}
883
884CORE_ADDR
885read_register_pid (regno, pid)
886     int regno, pid;
887{
888  int save_pid;
889  CORE_ADDR retval;
890
891  if (pid == inferior_pid)
892    return read_register (regno);
893
894  save_pid = inferior_pid;
895
896  inferior_pid = pid;
897
898  retval = read_register (regno);
899
900  inferior_pid = save_pid;
901
902  return retval;
903}
904
905/* Store VALUE, into the raw contents of register number REGNO.
906   This should probably write a LONGEST rather than a CORE_ADDR */
907
908void
909write_register (regno, val)
910     int regno;
911     LONGEST val;
912{
913  PTR buf;
914  int size;
915
916  /* On the sparc, writing %g0 is a no-op, so we don't even want to change
917     the registers array if something writes to this register.  */
918  if (CANNOT_STORE_REGISTER (regno))
919    return;
920
921  if (registers_pid != inferior_pid)
922    {
923      registers_changed ();
924      registers_pid = inferior_pid;
925    }
926
927  size = REGISTER_RAW_SIZE(regno);
928  buf = alloca (size);
929  store_signed_integer (buf, size, (LONGEST)val);
930
931  /* If we have a valid copy of the register, and new value == old value,
932     then don't bother doing the actual store. */
933
934  if (register_valid [regno]
935      && memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
936    return;
937
938  target_prepare_to_store ();
939
940  memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
941
942  register_valid [regno] = 1;
943
944  target_store_registers (regno);
945}
946
947void
948write_register_pid (regno, val, pid)
949     int regno;
950     CORE_ADDR val;
951     int pid;
952{
953  int save_pid;
954
955  if (pid == inferior_pid)
956    {
957      write_register (regno, val);
958      return;
959    }
960
961  save_pid = inferior_pid;
962
963  inferior_pid = pid;
964
965  write_register (regno, val);
966
967  inferior_pid = save_pid;
968}
969
970/* Record that register REGNO contains VAL.
971   This is used when the value is obtained from the inferior or core dump,
972   so there is no need to store the value there.
973
974   If VAL is a NULL pointer, then it's probably an unsupported register.  We
975   just set it's value to all zeros.  We might want to record this fact, and
976   report it to the users of read_register and friends.
977*/
978
979void
980supply_register (regno, val)
981     int regno;
982     char *val;
983{
984#if 1
985  if (registers_pid != inferior_pid)
986    {
987      registers_changed ();
988      registers_pid = inferior_pid;
989    }
990#endif
991
992  register_valid[regno] = 1;
993  if (val)
994    memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
995  else
996    memset (&registers[REGISTER_BYTE (regno)], '\000', REGISTER_RAW_SIZE (regno));
997
998  /* On some architectures, e.g. HPPA, there are a few stray bits in some
999     registers, that the rest of the code would like to ignore.  */
1000#ifdef CLEAN_UP_REGISTER_VALUE
1001  CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
1002#endif
1003}
1004
1005
1006/* This routine is getting awfully cluttered with #if's.  It's probably
1007   time to turn this into READ_PC and define it in the tm.h file.
1008   Ditto for write_pc.  */
1009
1010CORE_ADDR
1011read_pc_pid (pid)
1012     int pid;
1013{
1014  int  saved_inferior_pid;
1015  CORE_ADDR  pc_val;
1016
1017  /* In case pid != inferior_pid. */
1018  saved_inferior_pid = inferior_pid;
1019  inferior_pid = pid;
1020
1021#ifdef TARGET_READ_PC
1022  pc_val = TARGET_READ_PC (pid);
1023#else
1024  pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
1025#endif
1026
1027  inferior_pid = saved_inferior_pid;
1028  return pc_val;
1029}
1030
1031CORE_ADDR
1032read_pc ()
1033{
1034  return read_pc_pid (inferior_pid);
1035}
1036
1037void
1038write_pc_pid (pc, pid)
1039     CORE_ADDR pc;
1040     int pid;
1041{
1042  int  saved_inferior_pid;
1043
1044  /* In case pid != inferior_pid. */
1045  saved_inferior_pid = inferior_pid;
1046  inferior_pid = pid;
1047
1048#ifdef TARGET_WRITE_PC
1049  TARGET_WRITE_PC (pc, pid);
1050#else
1051  write_register_pid (PC_REGNUM, pc, pid);
1052#ifdef NPC_REGNUM
1053  write_register_pid (NPC_REGNUM, pc + 4, pid);
1054#ifdef NNPC_REGNUM
1055  write_register_pid (NNPC_REGNUM, pc + 8, pid);
1056#endif
1057#endif
1058#endif
1059
1060  inferior_pid = saved_inferior_pid;
1061}
1062
1063void
1064write_pc (pc)
1065     CORE_ADDR pc;
1066{
1067  write_pc_pid (pc, inferior_pid);
1068}
1069
1070/* Cope with strage ways of getting to the stack and frame pointers */
1071
1072CORE_ADDR
1073read_sp ()
1074{
1075#ifdef TARGET_READ_SP
1076  return TARGET_READ_SP ();
1077#else
1078  return read_register (SP_REGNUM);
1079#endif
1080}
1081
1082void
1083write_sp (val)
1084     CORE_ADDR val;
1085{
1086#ifdef TARGET_WRITE_SP
1087  TARGET_WRITE_SP (val);
1088#else
1089  write_register (SP_REGNUM, val);
1090#endif
1091}
1092
1093CORE_ADDR
1094read_fp ()
1095{
1096#ifdef TARGET_READ_FP
1097  return TARGET_READ_FP ();
1098#else
1099  return read_register (FP_REGNUM);
1100#endif
1101}
1102
1103void
1104write_fp (val)
1105     CORE_ADDR val;
1106{
1107#ifdef TARGET_WRITE_FP
1108  TARGET_WRITE_FP (val);
1109#else
1110  write_register (FP_REGNUM, val);
1111#endif
1112}
1113
1114/* Will calling read_var_value or locate_var_value on SYM end
1115   up caring what frame it is being evaluated relative to?  SYM must
1116   be non-NULL.  */
1117int
1118symbol_read_needs_frame (sym)
1119     struct symbol *sym;
1120{
1121  switch (SYMBOL_CLASS (sym))
1122    {
1123      /* All cases listed explicitly so that gcc -Wall will detect it if
1124	 we failed to consider one.  */
1125    case LOC_REGISTER:
1126    case LOC_ARG:
1127    case LOC_REF_ARG:
1128    case LOC_REGPARM:
1129    case LOC_REGPARM_ADDR:
1130    case LOC_LOCAL:
1131    case LOC_LOCAL_ARG:
1132    case LOC_BASEREG:
1133    case LOC_BASEREG_ARG:
1134    case LOC_THREAD_LOCAL_STATIC:
1135      return 1;
1136
1137    case LOC_UNDEF:
1138    case LOC_CONST:
1139    case LOC_STATIC:
1140    case LOC_INDIRECT:
1141    case LOC_TYPEDEF:
1142
1143    case LOC_LABEL:
1144      /* Getting the address of a label can be done independently of the block,
1145	 even if some *uses* of that address wouldn't work so well without
1146	 the right frame.  */
1147
1148    case LOC_BLOCK:
1149    case LOC_CONST_BYTES:
1150    case LOC_UNRESOLVED:
1151    case LOC_OPTIMIZED_OUT:
1152      return 0;
1153    }
1154  return 1;
1155}
1156
1157/* Given a struct symbol for a variable,
1158   and a stack frame id, read the value of the variable
1159   and return a (pointer to a) struct value containing the value.
1160   If the variable cannot be found, return a zero pointer.
1161   If FRAME is NULL, use the selected_frame.  */
1162
1163value_ptr
1164read_var_value (var, frame)
1165     register struct symbol *var;
1166     struct frame_info *frame;
1167{
1168  register value_ptr v;
1169  struct type *type = SYMBOL_TYPE (var);
1170  CORE_ADDR addr;
1171  register int len;
1172
1173  v = allocate_value (type);
1174  VALUE_LVAL (v) = lval_memory;	/* The most likely possibility.  */
1175  VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
1176
1177  len = TYPE_LENGTH (type);
1178
1179  if (frame == NULL) frame = selected_frame;
1180
1181  switch (SYMBOL_CLASS (var))
1182    {
1183    case LOC_CONST:
1184      /* Put the constant back in target format.  */
1185      store_signed_integer (VALUE_CONTENTS_RAW (v), len,
1186			    (LONGEST) SYMBOL_VALUE (var));
1187      VALUE_LVAL (v) = not_lval;
1188      return v;
1189
1190    case LOC_LABEL:
1191      /* Put the constant back in target format.  */
1192      if (overlay_debugging)
1193	store_address (VALUE_CONTENTS_RAW (v), len,
1194		       (LONGEST)symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1195							   SYMBOL_BFD_SECTION (var)));
1196      else
1197	store_address (VALUE_CONTENTS_RAW (v), len,
1198		       (LONGEST)SYMBOL_VALUE_ADDRESS (var));
1199      VALUE_LVAL (v) = not_lval;
1200      return v;
1201
1202    case LOC_CONST_BYTES:
1203      {
1204	char *bytes_addr;
1205	bytes_addr = SYMBOL_VALUE_BYTES (var);
1206	memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
1207	VALUE_LVAL (v) = not_lval;
1208	return v;
1209      }
1210
1211    case LOC_STATIC:
1212      if (overlay_debugging)
1213	addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1214					 SYMBOL_BFD_SECTION (var));
1215      else
1216	addr = SYMBOL_VALUE_ADDRESS (var);
1217      break;
1218
1219    case LOC_INDIRECT:
1220      /* The import slot does not have a real address in it from the
1221         dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1222         execution yet, so check for that. */
1223      if (!target_has_execution)
1224        error ("\
1225Attempt to access variable defined in different shared object or load module when\n\
1226addresses have not been bound by the dynamic loader. Try again when executable is running.");
1227
1228      addr = SYMBOL_VALUE_ADDRESS (var);
1229      addr = read_memory_unsigned_integer
1230	(addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1231      break;
1232
1233    case LOC_ARG:
1234      if (frame == NULL)
1235	return 0;
1236      addr = FRAME_ARGS_ADDRESS (frame);
1237      if (!addr)
1238	return 0;
1239      addr += SYMBOL_VALUE (var);
1240      break;
1241
1242    case LOC_REF_ARG:
1243      if (frame == NULL)
1244	return 0;
1245      addr = FRAME_ARGS_ADDRESS (frame);
1246      if (!addr)
1247	return 0;
1248      addr += SYMBOL_VALUE (var);
1249      addr = read_memory_unsigned_integer
1250	(addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1251      break;
1252
1253    case LOC_LOCAL:
1254    case LOC_LOCAL_ARG:
1255      if (frame == NULL)
1256	return 0;
1257      addr = FRAME_LOCALS_ADDRESS (frame);
1258      addr += SYMBOL_VALUE (var);
1259      break;
1260
1261    case LOC_BASEREG:
1262    case LOC_BASEREG_ARG:
1263      {
1264	char buf[MAX_REGISTER_RAW_SIZE];
1265	get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1266			    NULL);
1267	addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1268	addr += SYMBOL_VALUE (var);
1269	break;
1270      }
1271
1272    case LOC_THREAD_LOCAL_STATIC:
1273      {
1274        char buf[MAX_REGISTER_RAW_SIZE];
1275
1276        get_saved_register(buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1277			    NULL);
1278        addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1279        addr += SYMBOL_VALUE (var );
1280        break;
1281      }
1282
1283    case LOC_TYPEDEF:
1284      error ("Cannot look up value of a typedef");
1285      break;
1286
1287    case LOC_BLOCK:
1288      if (overlay_debugging)
1289	VALUE_ADDRESS (v) = symbol_overlayed_address
1290	  (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
1291      else
1292	VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
1293      return v;
1294
1295    case LOC_REGISTER:
1296    case LOC_REGPARM:
1297    case LOC_REGPARM_ADDR:
1298      {
1299	struct block *b;
1300	int regno = SYMBOL_VALUE (var);
1301	value_ptr regval;
1302
1303	if (frame == NULL)
1304	  return 0;
1305	b = get_frame_block (frame);
1306
1307	if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
1308	  {
1309	    regval = value_from_register (lookup_pointer_type (type),
1310					  regno,
1311					  frame);
1312
1313	    if (regval == NULL)
1314	      error ("Value of register variable not available.");
1315
1316	    addr   = value_as_pointer (regval);
1317	    VALUE_LVAL (v) = lval_memory;
1318	  }
1319	else
1320	  {
1321	    regval = value_from_register (type, regno, frame);
1322
1323	    if (regval == NULL)
1324	      error ("Value of register variable not available.");
1325	    return regval;
1326	  }
1327      }
1328      break;
1329
1330    case LOC_UNRESOLVED:
1331      {
1332	struct minimal_symbol *msym;
1333
1334	msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
1335	if (msym == NULL)
1336	  return 0;
1337	if (overlay_debugging)
1338	  addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
1339					   SYMBOL_BFD_SECTION (msym));
1340	else
1341	  addr = SYMBOL_VALUE_ADDRESS (msym);
1342      }
1343      break;
1344
1345    case LOC_OPTIMIZED_OUT:
1346      VALUE_LVAL (v) = not_lval;
1347      VALUE_OPTIMIZED_OUT (v) = 1;
1348      return v;
1349
1350    default:
1351      error ("Cannot look up value of a botched symbol.");
1352      break;
1353    }
1354
1355  VALUE_ADDRESS (v) = addr;
1356  VALUE_LAZY (v) = 1;
1357  return v;
1358}
1359
1360/* Return a value of type TYPE, stored in register REGNUM, in frame
1361   FRAME.
1362
1363   NOTE: returns NULL if register value is not available.
1364   Caller will check return value or die!  */
1365
1366value_ptr
1367value_from_register (type, regnum, frame)
1368     struct type *type;
1369     int regnum;
1370     struct frame_info *frame;
1371{
1372  char raw_buffer [MAX_REGISTER_RAW_SIZE];
1373  CORE_ADDR addr;
1374  int optim;
1375  value_ptr v = allocate_value (type);
1376  char *value_bytes = 0;
1377  int value_bytes_copied = 0;
1378  int num_storage_locs;
1379  enum lval_type lval;
1380  int len;
1381
1382  CHECK_TYPEDEF (type);
1383  len = TYPE_LENGTH (type);
1384
1385  VALUE_REGNO (v) = regnum;
1386
1387  num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
1388		      ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
1389		      1);
1390
1391  if (num_storage_locs > 1
1392#ifdef GDB_TARGET_IS_H8500
1393      || TYPE_CODE (type) == TYPE_CODE_PTR
1394#endif
1395      )
1396    {
1397      /* Value spread across multiple storage locations.  */
1398
1399      int local_regnum;
1400      int mem_stor = 0, reg_stor = 0;
1401      int mem_tracking = 1;
1402      CORE_ADDR last_addr = 0;
1403      CORE_ADDR first_addr = 0;
1404
1405      value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1406
1407      /* Copy all of the data out, whereever it may be.  */
1408
1409#ifdef GDB_TARGET_IS_H8500
1410/* This piece of hideosity is required because the H8500 treats registers
1411   differently depending upon whether they are used as pointers or not.  As a
1412   pointer, a register needs to have a page register tacked onto the front.
1413   An alternate way to do this would be to have gcc output different register
1414   numbers for the pointer & non-pointer form of the register.  But, it
1415   doesn't, so we're stuck with this.  */
1416
1417      if (TYPE_CODE (type) == TYPE_CODE_PTR
1418	  && len > 2)
1419	{
1420	  int page_regnum;
1421
1422	  switch (regnum)
1423	    {
1424	    case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
1425	      page_regnum = SEG_D_REGNUM;
1426	      break;
1427	    case R4_REGNUM: case R5_REGNUM:
1428	      page_regnum = SEG_E_REGNUM;
1429	      break;
1430	    case R6_REGNUM: case R7_REGNUM:
1431	      page_regnum = SEG_T_REGNUM;
1432	      break;
1433	    }
1434
1435	  value_bytes[0] = 0;
1436	  get_saved_register (value_bytes + 1,
1437			      &optim,
1438			      &addr,
1439			      frame,
1440			      page_regnum,
1441			      &lval);
1442
1443	  if (register_valid[page_regnum] == -1)
1444	    return NULL;	/* register value not available */
1445
1446	  if (lval == lval_register)
1447	    reg_stor++;
1448	  else
1449	    mem_stor++;
1450	  first_addr = addr;
1451	  last_addr = addr;
1452
1453	  get_saved_register (value_bytes + 2,
1454			      &optim,
1455			      &addr,
1456			      frame,
1457			      regnum,
1458			      &lval);
1459
1460	  if (register_valid[regnum] == -1)
1461	    return NULL;	/* register value not available */
1462
1463	  if (lval == lval_register)
1464	    reg_stor++;
1465	  else
1466	    {
1467	      mem_stor++;
1468	      mem_tracking = mem_tracking && (addr == last_addr);
1469	    }
1470	  last_addr = addr;
1471	}
1472      else
1473#endif				/* GDB_TARGET_IS_H8500 */
1474	for (local_regnum = regnum;
1475	     value_bytes_copied < len;
1476	     (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
1477	      ++local_regnum))
1478	  {
1479	    get_saved_register (value_bytes + value_bytes_copied,
1480				&optim,
1481				&addr,
1482				frame,
1483				local_regnum,
1484				&lval);
1485
1486	  if (register_valid[local_regnum] == -1)
1487	    return NULL;	/* register value not available */
1488
1489	    if (regnum == local_regnum)
1490	      first_addr = addr;
1491	    if (lval == lval_register)
1492	      reg_stor++;
1493	    else
1494	      {
1495		mem_stor++;
1496
1497		mem_tracking =
1498		  (mem_tracking
1499		   && (regnum == local_regnum
1500		       || addr == last_addr));
1501	      }
1502	    last_addr = addr;
1503	  }
1504
1505      if ((reg_stor && mem_stor)
1506	  || (mem_stor && !mem_tracking))
1507	/* Mixed storage; all of the hassle we just went through was
1508	   for some good purpose.  */
1509	{
1510	  VALUE_LVAL (v) = lval_reg_frame_relative;
1511	  VALUE_FRAME (v) = FRAME_FP (frame);
1512	  VALUE_FRAME_REGNUM (v) = regnum;
1513	}
1514      else if (mem_stor)
1515	{
1516	  VALUE_LVAL (v) = lval_memory;
1517	  VALUE_ADDRESS (v) = first_addr;
1518	}
1519      else if (reg_stor)
1520	{
1521	  VALUE_LVAL (v) = lval_register;
1522	  VALUE_ADDRESS (v) = first_addr;
1523	}
1524      else
1525	fatal ("value_from_register: Value not stored anywhere!");
1526
1527      VALUE_OPTIMIZED_OUT (v) = optim;
1528
1529      /* Any structure stored in more than one register will always be
1530	 an integral number of registers.  Otherwise, you'd need to do
1531	 some fiddling with the last register copied here for little
1532	 endian machines.  */
1533
1534      /* Copy into the contents section of the value.  */
1535      memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
1536
1537      /* Finally do any conversion necessary when extracting this
1538         type from more than one register.  */
1539#ifdef REGISTER_CONVERT_TO_TYPE
1540      REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
1541#endif
1542      return v;
1543    }
1544
1545  /* Data is completely contained within a single register.  Locate the
1546     register's contents in a real register or in core;
1547     read the data in raw format.  */
1548
1549  get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
1550
1551  if (register_valid[regnum] == -1)
1552    return NULL;	/* register value not available */
1553
1554  VALUE_OPTIMIZED_OUT (v) = optim;
1555  VALUE_LVAL (v) = lval;
1556  VALUE_ADDRESS (v) = addr;
1557
1558  /* Convert raw data to virtual format if necessary.  */
1559
1560#ifdef REGISTER_CONVERTIBLE
1561  if (REGISTER_CONVERTIBLE (regnum))
1562    {
1563      REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1564				   raw_buffer, VALUE_CONTENTS_RAW (v));
1565    }
1566  else
1567#endif
1568    {
1569      /* Raw and virtual formats are the same for this register.  */
1570
1571      if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
1572	{
1573  	  /* Big-endian, and we want less than full size.  */
1574	  VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1575	}
1576
1577      memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
1578    }
1579
1580  return v;
1581}
1582
1583/* Given a struct symbol for a variable or function,
1584   and a stack frame id,
1585   return a (pointer to a) struct value containing the properly typed
1586   address.  */
1587
1588value_ptr
1589locate_var_value (var, frame)
1590     register struct symbol *var;
1591     struct frame_info *frame;
1592{
1593  CORE_ADDR addr = 0;
1594  struct type *type = SYMBOL_TYPE (var);
1595  value_ptr lazy_value;
1596
1597  /* Evaluate it first; if the result is a memory address, we're fine.
1598     Lazy evaluation pays off here. */
1599
1600  lazy_value = read_var_value (var, frame);
1601  if (lazy_value == 0)
1602    error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1603
1604  if (VALUE_LAZY (lazy_value)
1605      || TYPE_CODE (type) == TYPE_CODE_FUNC)
1606    {
1607      value_ptr val;
1608
1609      addr = VALUE_ADDRESS (lazy_value);
1610      val =  value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
1611      VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
1612      return val;
1613    }
1614
1615  /* Not a memory address; check what the problem was.  */
1616  switch (VALUE_LVAL (lazy_value))
1617    {
1618    case lval_register:
1619    case lval_reg_frame_relative:
1620      error ("Address requested for identifier \"%s\" which is in a register.",
1621	     SYMBOL_SOURCE_NAME (var));
1622      break;
1623
1624    default:
1625      error ("Can't take address of \"%s\" which isn't an lvalue.",
1626	     SYMBOL_SOURCE_NAME (var));
1627      break;
1628    }
1629  return 0;  /* For lint -- never reached */
1630}
1631