2002-11-13 Andrew Cagney <cagney@redhat.com>
[platform/upstream/binutils.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4    2001, 2002 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 "inferior.h"
25 #include "target.h"
26 #include "gdbarch.h"
27 #include "gdbcmd.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "gdbcmd.h"             /* For maintenanceprintlist.  */
33
34 /*
35  * DATA STRUCTURE
36  *
37  * Here is the actual register cache.
38  */
39
40 /* Per-architecture object describing the layout of a register cache.
41    Computed once when the architecture is created */
42
43 struct gdbarch_data *regcache_descr_handle;
44
45 struct regcache_descr
46 {
47   /* The architecture this descriptor belongs to.  */
48   struct gdbarch *gdbarch;
49
50   /* Is this a ``legacy'' register cache?  Such caches reserve space
51      for raw and pseudo registers and allow access to both.  */
52   int legacy_p;
53
54   /* The raw register cache.  This should contain just [0
55      .. NUM_RAW_REGISTERS).  However, for older targets, it contains
56      space for the full [0 .. NUM_RAW_REGISTERS +
57      NUM_PSEUDO_REGISTERS).  */
58   int nr_raw_registers;
59   long sizeof_raw_registers;
60   long sizeof_raw_register_valid_p;
61
62   /* The cooked register space.  Each cooked register in the range
63      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
64      register.  The remaining [NR_RAW_REGISTERS
65      .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
66      both raw registers and memory by the architecture methods
67      gdbarch_register_read and gdbarch_register_write.  */
68   int nr_cooked_registers;
69   long sizeof_cooked_registers;
70   long sizeof_cooked_register_valid_p;
71
72   /* Offset and size (in 8 bit bytes), of reach register in the
73      register cache.  All registers (including those in the range
74      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
75      Assigning all registers an offset makes it possible to keep
76      legacy code, such as that found in read_register_bytes() and
77      write_register_bytes() working.  */
78   long *register_offset;
79   long *sizeof_register;
80
81   /* Useful constant.  Largest of all the registers.  */
82   long max_register_size;
83
84   /* Cached table containing the type of each register.  */
85   struct type **register_type;
86 };
87
88 void
89 init_legacy_regcache_descr (struct gdbarch *gdbarch,
90                             struct regcache_descr *descr)
91 {
92   int i;
93   /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
94      ``gdbarch'' as a parameter.  */
95   gdb_assert (gdbarch != NULL);
96
97   /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
98      in the register cache.  Unfortunatly some architectures still
99      rely on this and the pseudo_register_write() method.  */
100   descr->nr_raw_registers = descr->nr_cooked_registers;
101   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
102
103   /* Compute the offset of each register.  Legacy architectures define
104      REGISTER_BYTE() so use that.  */
105   /* FIXME: cagney/2002-11-07: Instead of using REGISTER_BYTE() this
106      code should, as is done in init_regcache_descr(), compute the
107      offets at runtime.  This currently isn't possible as some ISAs
108      define overlapping register regions - see the mess in
109      read_register_bytes() and write_register_bytes() registers.  */
110   descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
111   descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
112   descr->max_register_size = 0;
113   for (i = 0; i < descr->nr_cooked_registers; i++)
114     {
115       /* FIXME: cagney/2001-12-04: This code shouldn't need to use
116          REGISTER_BYTE().  Unfortunatly, legacy code likes to lay the
117          buffer out so that certain registers just happen to overlap.
118          Ulgh!  New targets use gdbarch's register read/write and
119          entirely avoid this uglyness.  */
120       descr->register_offset[i] = REGISTER_BYTE (i);
121       descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
122       if (descr->max_register_size < REGISTER_RAW_SIZE (i))
123         descr->max_register_size = REGISTER_RAW_SIZE (i);
124       if (descr->max_register_size < REGISTER_VIRTUAL_SIZE (i))
125         descr->max_register_size = REGISTER_VIRTUAL_SIZE (i);
126     }
127
128   /* Compute the real size of the register buffer.  Start out by
129      trusting REGISTER_BYTES, but then adjust it upwards should that
130      be found to not be sufficient.  */
131   /* FIXME: cagney/2002-11-05: Instead of using REGISTER_BYTES, this
132      code should, as is done in init_regcache_descr(), compute the
133      total number of register bytes using the accumulated offsets.  */
134   descr->sizeof_cooked_registers = REGISTER_BYTES; /* OK use.  */
135   for (i = 0; i < descr->nr_cooked_registers; i++)
136     {
137       long regend;
138       /* Keep extending the buffer so that there is always enough
139          space for all registers.  The comparison is necessary since
140          legacy code is free to put registers in random places in the
141          buffer separated by holes.  Once REGISTER_BYTE() is killed
142          this can be greatly simplified.  */
143       regend = descr->register_offset[i] + descr->sizeof_register[i];
144       if (descr->sizeof_cooked_registers < regend)
145         descr->sizeof_cooked_registers = regend;
146     }
147   /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
148      in the register cache.  Unfortunatly some architectures still
149      rely on this and the pseudo_register_write() method.  */
150   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
151 }
152
153 static void *
154 init_regcache_descr (struct gdbarch *gdbarch)
155 {
156   int i;
157   struct regcache_descr *descr;
158   gdb_assert (gdbarch != NULL);
159
160   /* Create an initial, zero filled, table.  */
161   descr = XCALLOC (1, struct regcache_descr);
162   descr->gdbarch = gdbarch;
163
164   /* Total size of the register space.  The raw registers are mapped
165      directly onto the raw register cache while the pseudo's are
166      either mapped onto raw-registers or memory.  */
167   descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
168   descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
169
170   /* Fill in a table of register types.  */
171   descr->register_type = XCALLOC (descr->nr_cooked_registers,
172                                   struct type *);
173   for (i = 0; i < descr->nr_cooked_registers; i++)
174     {
175       descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i);
176     }
177
178   /* If an old style architecture, fill in the remainder of the
179      register cache descriptor using the register macros.  */
180   if (!gdbarch_pseudo_register_read_p (gdbarch)
181       && !gdbarch_pseudo_register_write_p (gdbarch))
182     {
183       descr->legacy_p = 1;
184       init_legacy_regcache_descr (gdbarch, descr);
185       return descr;
186     }
187
188   /* Construct a strictly RAW register cache.  Don't allow pseudo's
189      into the register cache.  */
190   descr->nr_raw_registers = NUM_REGS;
191
192   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
193      array.  This pretects GDB from erant code that accesses elements
194      of the global register_valid_p[] array in the range [NUM_REGS
195      .. NUM_REGS + NUM_PSEUDO_REGS).  */
196   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
197
198   /* Lay out the register cache.
199
200      NOTE: cagney/2002-05-22: Only register_type() is used when
201      constructing the register cache.  It is assumed that the
202      register's raw size, virtual size and type length are all the
203      same.  */
204
205   {
206     long offset = 0;
207     descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
208     descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
209     descr->max_register_size = 0;
210     for (i = 0; i < descr->nr_cooked_registers; i++)
211       {
212         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
213         descr->register_offset[i] = offset;
214         offset += descr->sizeof_register[i];
215         if (descr->max_register_size < descr->sizeof_register[i])
216           descr->max_register_size = descr->sizeof_register[i];
217       }
218     /* Set the real size of the register cache buffer.  */
219     descr->sizeof_cooked_registers = offset;
220   }
221
222   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
223      the raw registers.  Unfortunatly some code still accesses the
224      register array directly using the global registers[].  Until that
225      code has been purged, play safe and over allocating the register
226      buffer.  Ulgh!  */
227   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
228
229 #if 0
230   /* Sanity check.  Confirm that the assumptions about gdbarch are
231      true.  The REGCACHE_DESCR_HANDLE is set before doing the checks
232      so that targets using the generic methods supplied by regcache
233      don't go into infinite recursion trying to, again, create the
234      regcache.  */
235   set_gdbarch_data (gdbarch, regcache_descr_handle, descr);
236   for (i = 0; i < descr->nr_cooked_registers; i++)
237     {
238       gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
239       gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
240       gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
241     }
242   /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i));  */
243 #endif
244
245   return descr;
246 }
247
248 static struct regcache_descr *
249 regcache_descr (struct gdbarch *gdbarch)
250 {
251   return gdbarch_data (gdbarch, regcache_descr_handle);
252 }
253
254 static void
255 xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
256 {
257   struct regcache_descr *descr = ptr;
258   if (descr == NULL)
259     return;
260   xfree (descr->register_offset);
261   xfree (descr->sizeof_register);
262   descr->register_offset = NULL;
263   descr->sizeof_register = NULL;
264   xfree (descr);
265 }
266
267 /* Utility functions returning useful register attributes stored in
268    the regcache descr.  */
269
270 struct type *
271 register_type (struct gdbarch *gdbarch, int regnum)
272 {
273   struct regcache_descr *descr = regcache_descr (gdbarch);
274   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
275   return descr->register_type[regnum];
276 }
277
278 /* Utility functions returning useful register attributes stored in
279    the regcache descr.  */
280
281 int
282 max_register_size (struct gdbarch *gdbarch)
283 {
284   struct regcache_descr *descr = regcache_descr (gdbarch);
285   return descr->max_register_size;
286 }
287
288 /* The register cache for storing raw register values.  */
289
290 struct regcache
291 {
292   struct regcache_descr *descr;
293   /* The register buffers.  A read-only register cache can hold the
294      full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
295      register cache can only hold [0 .. NUM_REGS).  */
296   char *registers;
297   char *register_valid_p;
298   /* Is this a read-only cache?  A read-only cache is used for saving
299      the target's register state (e.g, across an inferior function
300      call or just before forcing a function return).  A read-only
301      cache can only be updated via the methods regcache_dup() and
302      regcache_cpy().  The actual contents are determined by the
303      reggroup_save and reggroup_restore methods.  */
304   int readonly_p;
305 };
306
307 struct regcache *
308 regcache_xmalloc (struct gdbarch *gdbarch)
309 {
310   struct regcache_descr *descr;
311   struct regcache *regcache;
312   gdb_assert (gdbarch != NULL);
313   descr = regcache_descr (gdbarch);
314   regcache = XMALLOC (struct regcache);
315   regcache->descr = descr;
316   regcache->registers
317     = XCALLOC (descr->sizeof_raw_registers, char);
318   regcache->register_valid_p
319     = XCALLOC (descr->sizeof_raw_register_valid_p, char);
320   regcache->readonly_p = 1;
321   return regcache;
322 }
323
324 void
325 regcache_xfree (struct regcache *regcache)
326 {
327   if (regcache == NULL)
328     return;
329   xfree (regcache->registers);
330   xfree (regcache->register_valid_p);
331   xfree (regcache);
332 }
333
334 void
335 do_regcache_xfree (void *data)
336 {
337   regcache_xfree (data);
338 }
339
340 struct cleanup *
341 make_cleanup_regcache_xfree (struct regcache *regcache)
342 {
343   return make_cleanup (do_regcache_xfree, regcache);
344 }
345
346 /* Return  a pointer to register REGNUM's buffer cache.  */
347
348 static char *
349 register_buffer (struct regcache *regcache, int regnum)
350 {
351   return regcache->registers + regcache->descr->register_offset[regnum];
352 }
353
354 void
355 regcache_save (struct regcache *dst, struct regcache *src)
356 {
357   struct gdbarch *gdbarch = dst->descr->gdbarch;
358   int regnum;
359   /* The SRC and DST register caches had better belong to the same
360      architecture.  */
361   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
362   /* The DST should be `read-only', if it wasn't then the save would
363      end up trying to write the register values out through to the
364      target.  */
365   gdb_assert (!src->readonly_p);
366   gdb_assert (dst->readonly_p);
367   /* Clear the dest.  */
368   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
369   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
370   /* Copy over any registers (identified by their membership in the
371      save_reggroup) and mark them as valid.  The full [0
372      .. NUM_REGS+NUM_PSEUDO_REGS) range is checked since some
373      architectures need to save/restore `cooked' registers that live
374      in memory.  */
375   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
376     {
377       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
378         {
379           regcache_cooked_read (src, regnum, register_buffer (dst, regnum));
380           dst->register_valid_p[regnum] = 1;
381         }
382     }
383 }
384
385 void
386 regcache_restore (struct regcache *dst, struct regcache *src)
387 {
388   struct gdbarch *gdbarch = dst->descr->gdbarch;
389   int regnum;
390   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
391   gdb_assert (!dst->readonly_p);
392   gdb_assert (src->readonly_p);
393   /* Copy over any registers, being careful to only restore those that
394      were both saved and need to be restored.  The full [0
395      .. NUM_REGS+NUM_PSEUDO_REGS) range is checked since some
396      architectures need to save/restore `cooked' registers that live
397      in memory.  */
398   for (regnum = 0; regnum < src->descr->nr_cooked_registers; regnum++)
399     {
400       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)
401           && src->register_valid_p[regnum])
402         {
403           regcache_cooked_write (dst, regnum, register_buffer (src, regnum));
404         }
405     }
406 }
407
408 void
409 regcache_cpy (struct regcache *dst, struct regcache *src)
410 {
411   int i;
412   char *buf;
413   gdb_assert (src != NULL && dst != NULL);
414   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
415   gdb_assert (src != dst);
416   gdb_assert (src->readonly_p || dst->readonly_p);
417   if (!src->readonly_p)
418     regcache_save (dst, src);
419   else if (!dst->readonly_p)
420     regcache_restore (dst, src);
421   else
422     regcache_cpy_no_passthrough (dst, src);
423 }
424
425 void
426 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
427 {
428   int i;
429   gdb_assert (src != NULL && dst != NULL);
430   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
431   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
432      move of data into the current_regcache().  Doing this would be
433      silly - it would mean that valid_p would be completly invalid.  */
434   gdb_assert (dst != current_regcache);
435   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
436   memcpy (dst->register_valid_p, src->register_valid_p,
437           dst->descr->sizeof_raw_register_valid_p);
438 }
439
440 struct regcache *
441 regcache_dup (struct regcache *src)
442 {
443   struct regcache *newbuf;
444   gdb_assert (current_regcache != NULL);
445   newbuf = regcache_xmalloc (src->descr->gdbarch);
446   regcache_cpy (newbuf, src);
447   return newbuf;
448 }
449
450 struct regcache *
451 regcache_dup_no_passthrough (struct regcache *src)
452 {
453   struct regcache *newbuf;
454   gdb_assert (current_regcache != NULL);
455   newbuf = regcache_xmalloc (src->descr->gdbarch);
456   regcache_cpy_no_passthrough (newbuf, src);
457   return newbuf;
458 }
459
460 int
461 regcache_valid_p (struct regcache *regcache, int regnum)
462 {
463   gdb_assert (regcache != NULL);
464   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
465   return regcache->register_valid_p[regnum];
466 }
467
468 char *
469 deprecated_grub_regcache_for_registers (struct regcache *regcache)
470 {
471   return regcache->registers;
472 }
473
474 char *
475 deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
476 {
477   return regcache->register_valid_p;
478 }
479
480 /* Global structure containing the current regcache.  */
481 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
482    deprecated_register_valid[] currently point into this structure.  */
483 struct regcache *current_regcache;
484
485 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
486    recording if the register values have been changed (eg. by the
487    user).  Therefore all registers must be written back to the
488    target when appropriate.  */
489
490 /* REGISTERS contains the cached register values (in target byte order). */
491
492 char *registers;
493
494 /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
495                      1 if it has been fetched, and
496                     -1 if the register value was not available.  
497
498    "Not available" indicates that the target is not not able to supply
499    the register at this state.  The register may become available at a
500    later time (after the next resume).  This often occures when GDB is
501    manipulating a target that contains only a snapshot of the entire
502    system being debugged - some of the registers in such a system may
503    not have been saved.  */
504
505 signed char *deprecated_register_valid;
506
507 /* The thread/process associated with the current set of registers. */
508
509 static ptid_t registers_ptid;
510
511 /*
512  * FUNCTIONS:
513  */
514
515 /* REGISTER_CACHED()
516
517    Returns 0 if the value is not in the cache (needs fetch).
518           >0 if the value is in the cache.
519           <0 if the value is permanently unavailable (don't ask again).  */
520
521 int
522 register_cached (int regnum)
523 {
524   return deprecated_register_valid[regnum];
525 }
526
527 /* Record that REGNUM's value is cached if STATE is >0, uncached but
528    fetchable if STATE is 0, and uncached and unfetchable if STATE is <0.  */
529
530 void
531 set_register_cached (int regnum, int state)
532 {
533   gdb_assert (regnum >= 0);
534   gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
535   current_regcache->register_valid_p[regnum] = state;
536 }
537
538 /* Return whether register REGNUM is a real register.  */
539
540 static int
541 real_register (int regnum)
542 {
543   return regnum >= 0 && regnum < NUM_REGS;
544 }
545
546 /* Low level examining and depositing of registers.
547
548    The caller is responsible for making sure that the inferior is
549    stopped before calling the fetching routines, or it will get
550    garbage.  (a change from GDB version 3, in which the caller got the
551    value from the last stop).  */
552
553 /* REGISTERS_CHANGED ()
554
555    Indicate that registers may have changed, so invalidate the cache.  */
556
557 void
558 registers_changed (void)
559 {
560   int i;
561
562   registers_ptid = pid_to_ptid (-1);
563
564   /* Force cleanup of any alloca areas if using C alloca instead of
565      a builtin alloca.  This particular call is used to clean up
566      areas allocated by low level target code which may build up
567      during lengthy interactions between gdb and the target before
568      gdb gives control to the user (ie watchpoints).  */
569   alloca (0);
570
571   for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
572     set_register_cached (i, 0);
573
574   if (registers_changed_hook)
575     registers_changed_hook ();
576 }
577
578 /* DEPRECATED_REGISTERS_FETCHED ()
579
580    Indicate that all registers have been fetched, so mark them all valid.  */
581
582 /* NOTE: cagney/2001-12-04: This function does not set valid on the
583    pseudo-register range since pseudo registers are always supplied
584    using supply_register().  */
585 /* FIXME: cagney/2001-12-04: This function is DEPRECATED.  The target
586    code was blatting the registers[] array and then calling this.
587    Since targets should only be using supply_register() the need for
588    this function/hack is eliminated.  */
589
590 void
591 deprecated_registers_fetched (void)
592 {
593   int i;
594
595   for (i = 0; i < NUM_REGS; i++)
596     set_register_cached (i, 1);
597   /* Do not assume that the pseudo-regs have also been fetched.
598      Fetching all real regs NEVER accounts for pseudo-regs.  */
599 }
600
601 /* deprecated_read_register_bytes and deprecated_write_register_bytes
602    are generally a *BAD* idea.  They are inefficient because they need
603    to check for partial updates, which can only be done by scanning
604    through all of the registers and seeing if the bytes that are being
605    read/written fall inside of an invalid register.  [The main reason
606    this is necessary is that register sizes can vary, so a simple
607    index won't suffice.]  It is far better to call read_register_gen
608    and write_register_gen if you want to get at the raw register
609    contents, as it only takes a regnum as an argument, and therefore
610    can't do a partial register update.
611
612    Prior to the recent fixes to check for partial updates, both read
613    and deprecated_write_register_bytes always checked to see if any
614    registers were stale, and then called target_fetch_registers (-1)
615    to update the whole set.  This caused really slowed things down for
616    remote targets.  */
617
618 /* Copy INLEN bytes of consecutive data from registers
619    starting with the INREGBYTE'th byte of register data
620    into memory at MYADDR.  */
621
622 void
623 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
624 {
625   int in_end = in_start + in_len;
626   int regnum;
627   char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
628
629   /* See if we are trying to read bytes from out-of-date registers.  If so,
630      update just those registers.  */
631
632   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
633     {
634       int reg_start;
635       int reg_end;
636       int reg_len;
637       int start;
638       int end;
639       int byte;
640
641       reg_start = REGISTER_BYTE (regnum);
642       reg_len = REGISTER_RAW_SIZE (regnum);
643       reg_end = reg_start + reg_len;
644
645       if (reg_end <= in_start || in_end <= reg_start)
646         /* The range the user wants to read doesn't overlap with regnum.  */
647         continue;
648
649       if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
650         /* Force the cache to fetch the entire register.  */
651         deprecated_read_register_gen (regnum, reg_buf);
652       else
653         /* Legacy note: even though this register is ``invalid'' we
654            still need to return something.  It would appear that some
655            code relies on apparent gaps in the register array also
656            being returned.  */
657         /* FIXME: cagney/2001-08-18: This is just silly.  It defeats
658            the entire register read/write flow of control.  Must
659            resist temptation to return 0xdeadbeef.  */
660         memcpy (reg_buf, registers + reg_start, reg_len);
661
662       /* Legacy note: This function, for some reason, allows a NULL
663          input buffer.  If the buffer is NULL, the registers are still
664          fetched, just the final transfer is skipped. */
665       if (in_buf == NULL)
666         continue;
667
668       /* start = max (reg_start, in_start) */
669       if (reg_start > in_start)
670         start = reg_start;
671       else
672         start = in_start;
673
674       /* end = min (reg_end, in_end) */
675       if (reg_end < in_end)
676         end = reg_end;
677       else
678         end = in_end;
679
680       /* Transfer just the bytes common to both IN_BUF and REG_BUF */
681       for (byte = start; byte < end; byte++)
682         {
683           in_buf[byte - in_start] = reg_buf[byte - reg_start];
684         }
685     }
686 }
687
688 /* Read register REGNUM into memory at MYADDR, which must be large
689    enough for REGISTER_RAW_BYTES (REGNUM).  Target byte-order.  If the
690    register is known to be the size of a CORE_ADDR or smaller,
691    read_register can be used instead.  */
692
693 static void
694 legacy_read_register_gen (int regnum, char *myaddr)
695 {
696   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
697   if (! ptid_equal (registers_ptid, inferior_ptid))
698     {
699       registers_changed ();
700       registers_ptid = inferior_ptid;
701     }
702
703   if (!register_cached (regnum))
704     target_fetch_registers (regnum);
705
706   memcpy (myaddr, register_buffer (current_regcache, regnum),
707           REGISTER_RAW_SIZE (regnum));
708 }
709
710 void
711 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
712 {
713   gdb_assert (regcache != NULL && buf != NULL);
714   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
715   if (regcache->descr->legacy_p
716       && !regcache->readonly_p)
717     {
718       gdb_assert (regcache == current_regcache);
719       /* For moment, just use underlying legacy code.  Ulgh!!! This
720          silently and very indirectly updates the regcache's regcache
721          via the global deprecated_register_valid[].  */
722       legacy_read_register_gen (regnum, buf);
723       return;
724     }
725   /* Make certain that the register cache is up-to-date with respect
726      to the current thread.  This switching shouldn't be necessary
727      only there is still only one target side register cache.  Sigh!
728      On the bright side, at least there is a regcache object.  */
729   if (!regcache->readonly_p)
730     {
731       gdb_assert (regcache == current_regcache);
732       if (! ptid_equal (registers_ptid, inferior_ptid))
733         {
734           registers_changed ();
735           registers_ptid = inferior_ptid;
736         }
737       if (!register_cached (regnum))
738         target_fetch_registers (regnum);
739     }
740   /* Copy the value directly into the register cache.  */
741   memcpy (buf, register_buffer (regcache, regnum),
742           regcache->descr->sizeof_register[regnum]);
743 }
744
745 void
746 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
747 {
748   char *buf;
749   gdb_assert (regcache != NULL);
750   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
751   buf = alloca (regcache->descr->sizeof_register[regnum]);
752   regcache_raw_read (regcache, regnum, buf);
753   (*val) = extract_signed_integer (buf,
754                                    regcache->descr->sizeof_register[regnum]);
755 }
756
757 void
758 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
759                             ULONGEST *val)
760 {
761   char *buf;
762   gdb_assert (regcache != NULL);
763   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
764   buf = alloca (regcache->descr->sizeof_register[regnum]);
765   regcache_raw_read (regcache, regnum, buf);
766   (*val) = extract_unsigned_integer (buf,
767                                      regcache->descr->sizeof_register[regnum]);
768 }
769
770 void
771 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
772 {
773   void *buf;
774   gdb_assert (regcache != NULL);
775   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
776   buf = alloca (regcache->descr->sizeof_register[regnum]);
777   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
778   regcache_raw_write (regcache, regnum, buf);
779 }
780
781 void
782 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
783                              ULONGEST val)
784 {
785   void *buf;
786   gdb_assert (regcache != NULL);
787   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
788   buf = alloca (regcache->descr->sizeof_register[regnum]);
789   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
790   regcache_raw_write (regcache, regnum, buf);
791 }
792
793 void
794 deprecated_read_register_gen (int regnum, char *buf)
795 {
796   gdb_assert (current_regcache != NULL);
797   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
798   if (current_regcache->descr->legacy_p)
799     {
800       legacy_read_register_gen (regnum, buf);
801       return;
802     }
803   regcache_cooked_read (current_regcache, regnum, buf);
804 }
805
806 void
807 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
808 {
809   gdb_assert (regnum >= 0);
810   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
811   if (regnum < regcache->descr->nr_raw_registers)
812     regcache_raw_read (regcache, regnum, buf);
813   else if (regcache->readonly_p
814            && regnum < regcache->descr->nr_cooked_registers
815            && regcache->register_valid_p[regnum])
816     /* Read-only register cache, perhaphs the cooked value was cached?  */
817     memcpy (buf, register_buffer (regcache, regnum),
818             regcache->descr->sizeof_register[regnum]);
819   else
820     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
821                                   regnum, buf);
822 }
823
824 void
825 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
826                              LONGEST *val)
827 {
828   char *buf;
829   gdb_assert (regcache != NULL);
830   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
831   buf = alloca (regcache->descr->sizeof_register[regnum]);
832   regcache_cooked_read (regcache, regnum, buf);
833   (*val) = extract_signed_integer (buf,
834                                    regcache->descr->sizeof_register[regnum]);
835 }
836
837 void
838 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
839                                ULONGEST *val)
840 {
841   char *buf;
842   gdb_assert (regcache != NULL);
843   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
844   buf = alloca (regcache->descr->sizeof_register[regnum]);
845   regcache_cooked_read (regcache, regnum, buf);
846   (*val) = extract_unsigned_integer (buf,
847                                      regcache->descr->sizeof_register[regnum]);
848 }
849
850 /* Write register REGNUM at MYADDR to the target.  MYADDR points at
851    REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order.  */
852
853 static void
854 legacy_write_register_gen (int regnum, const void *myaddr)
855 {
856   int size;
857   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
858
859   /* On the sparc, writing %g0 is a no-op, so we don't even want to
860      change the registers array if something writes to this register.  */
861   if (CANNOT_STORE_REGISTER (regnum))
862     return;
863
864   if (! ptid_equal (registers_ptid, inferior_ptid))
865     {
866       registers_changed ();
867       registers_ptid = inferior_ptid;
868     }
869
870   size = REGISTER_RAW_SIZE (regnum);
871
872   if (real_register (regnum))
873     {
874       /* If we have a valid copy of the register, and new value == old
875          value, then don't bother doing the actual store. */
876       if (register_cached (regnum)
877           && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
878               == 0))
879         return;
880       else
881         target_prepare_to_store ();
882     }
883
884   memcpy (register_buffer (current_regcache, regnum), myaddr, size);
885
886   set_register_cached (regnum, 1);
887   target_store_registers (regnum);
888 }
889
890 void
891 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
892 {
893   gdb_assert (regcache != NULL && buf != NULL);
894   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
895   gdb_assert (!regcache->readonly_p);
896
897   if (regcache->descr->legacy_p)
898     {
899       /* For moment, just use underlying legacy code.  Ulgh!!! This
900          silently and very indirectly updates the regcache's buffers
901          via the globals deprecated_register_valid[] and registers[].  */
902       gdb_assert (regcache == current_regcache);
903       legacy_write_register_gen (regnum, buf);
904       return;
905     }
906
907   /* On the sparc, writing %g0 is a no-op, so we don't even want to
908      change the registers array if something writes to this register.  */
909   if (CANNOT_STORE_REGISTER (regnum))
910     return;
911
912   /* Make certain that the correct cache is selected.  */
913   gdb_assert (regcache == current_regcache);
914   if (! ptid_equal (registers_ptid, inferior_ptid))
915     {
916       registers_changed ();
917       registers_ptid = inferior_ptid;
918     }
919
920   /* If we have a valid copy of the register, and new value == old
921      value, then don't bother doing the actual store. */
922   if (regcache_valid_p (regcache, regnum)
923       && (memcmp (register_buffer (regcache, regnum), buf,
924                   regcache->descr->sizeof_register[regnum]) == 0))
925     return;
926
927   target_prepare_to_store ();
928   memcpy (register_buffer (regcache, regnum), buf,
929           regcache->descr->sizeof_register[regnum]);
930   regcache->register_valid_p[regnum] = 1;
931   target_store_registers (regnum);
932 }
933
934 void
935 deprecated_write_register_gen (int regnum, char *buf)
936 {
937   gdb_assert (current_regcache != NULL);
938   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
939   if (current_regcache->descr->legacy_p)
940     {
941       legacy_write_register_gen (regnum, buf);
942       return;
943     }
944   regcache_cooked_write (current_regcache, regnum, buf);
945 }
946
947 void
948 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
949 {
950   gdb_assert (regnum >= 0);
951   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
952   if (regnum < regcache->descr->nr_raw_registers)
953     regcache_raw_write (regcache, regnum, buf);
954   else
955     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
956                                    regnum, buf);
957 }
958
959 /* Copy INLEN bytes of consecutive data from memory at MYADDR
960    into registers starting with the MYREGSTART'th byte of register data.  */
961
962 void
963 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
964 {
965   int myregend = myregstart + inlen;
966   int regnum;
967
968   target_prepare_to_store ();
969
970   /* Scan through the registers updating any that are covered by the
971      range myregstart<=>myregend using write_register_gen, which does
972      nice things like handling threads, and avoiding updates when the
973      new and old contents are the same.  */
974
975   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
976     {
977       int regstart, regend;
978
979       regstart = REGISTER_BYTE (regnum);
980       regend = regstart + REGISTER_RAW_SIZE (regnum);
981
982       /* Is this register completely outside the range the user is writing?  */
983       if (myregend <= regstart || regend <= myregstart)
984         /* do nothing */ ;              
985
986       /* Is this register completely within the range the user is writing?  */
987       else if (myregstart <= regstart && regend <= myregend)
988         deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
989
990       /* The register partially overlaps the range being written.  */
991       else
992         {
993           char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
994           /* What's the overlap between this register's bytes and
995              those the caller wants to write?  */
996           int overlapstart = max (regstart, myregstart);
997           int overlapend   = min (regend,   myregend);
998
999           /* We may be doing a partial update of an invalid register.
1000              Update it from the target before scribbling on it.  */
1001           deprecated_read_register_gen (regnum, regbuf);
1002
1003           memcpy (registers + overlapstart,
1004                   myaddr + (overlapstart - myregstart),
1005                   overlapend - overlapstart);
1006
1007           target_store_registers (regnum);
1008         }
1009     }
1010 }
1011
1012 /* Perform a partial register transfer using a read, modify, write
1013    operation.  */
1014
1015 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1016                                     void *buf);
1017 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1018                                      const void *buf);
1019
1020 void
1021 regcache_xfer_part (struct regcache *regcache, int regnum,
1022                     int offset, int len, void *in, const void *out,
1023                     regcache_read_ftype *read, regcache_write_ftype *write)
1024 {
1025   struct regcache_descr *descr = regcache->descr;
1026   bfd_byte *reg = alloca (descr->max_register_size);
1027   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1028   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1029   /* Something to do?  */
1030   if (offset + len == 0)
1031     return;
1032   /* Read (when needed) ... */
1033   if (in != NULL
1034       || offset > 0
1035       || offset + len < descr->sizeof_register[regnum])
1036     {
1037       gdb_assert (read != NULL);
1038       read (regcache, regnum, reg);
1039     }
1040   /* ... modify ... */
1041   if (in != NULL)
1042     memcpy (in, reg + offset, len);
1043   if (out != NULL)
1044     memcpy (reg + offset, out, len);
1045   /* ... write (when needed).  */
1046   if (out != NULL)
1047     {
1048       gdb_assert (write != NULL);
1049       write (regcache, regnum, reg);
1050     }
1051 }
1052
1053 void
1054 regcache_raw_read_part (struct regcache *regcache, int regnum,
1055                         int offset, int len, void *buf)
1056 {
1057   struct regcache_descr *descr = regcache->descr;
1058   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1059   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1060                       regcache_raw_read, regcache_raw_write);
1061 }
1062
1063 void
1064 regcache_raw_write_part (struct regcache *regcache, int regnum,
1065                          int offset, int len, const void *buf)
1066 {
1067   struct regcache_descr *descr = regcache->descr;
1068   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1069   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1070                       regcache_raw_read, regcache_raw_write);
1071 }
1072
1073 void
1074 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1075                            int offset, int len, void *buf)
1076 {
1077   struct regcache_descr *descr = regcache->descr;
1078   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1079   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1080                       regcache_cooked_read, regcache_cooked_write);
1081 }
1082
1083 void
1084 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1085                             int offset, int len, const void *buf)
1086 {
1087   struct regcache_descr *descr = regcache->descr;
1088   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1089   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1090                       regcache_cooked_read, regcache_cooked_write);
1091 }
1092
1093 /* Hack to keep code that view the register buffer as raw bytes
1094    working.  */
1095
1096 int
1097 register_offset_hack (struct gdbarch *gdbarch, int regnum)
1098 {
1099   struct regcache_descr *descr = regcache_descr (gdbarch);
1100   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1101   return descr->register_offset[regnum];
1102 }
1103
1104 /* Return the contents of register REGNUM as an unsigned integer.  */
1105
1106 ULONGEST
1107 read_register (int regnum)
1108 {
1109   char *buf = alloca (REGISTER_RAW_SIZE (regnum));
1110   deprecated_read_register_gen (regnum, buf);
1111   return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
1112 }
1113
1114 ULONGEST
1115 read_register_pid (int regnum, ptid_t ptid)
1116 {
1117   ptid_t save_ptid;
1118   int save_pid;
1119   CORE_ADDR retval;
1120
1121   if (ptid_equal (ptid, inferior_ptid))
1122     return read_register (regnum);
1123
1124   save_ptid = inferior_ptid;
1125
1126   inferior_ptid = ptid;
1127
1128   retval = read_register (regnum);
1129
1130   inferior_ptid = save_ptid;
1131
1132   return retval;
1133 }
1134
1135 /* Return the contents of register REGNUM as a signed integer.  */
1136
1137 LONGEST
1138 read_signed_register (int regnum)
1139 {
1140   void *buf = alloca (REGISTER_RAW_SIZE (regnum));
1141   deprecated_read_register_gen (regnum, buf);
1142   return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
1143 }
1144
1145 LONGEST
1146 read_signed_register_pid (int regnum, ptid_t ptid)
1147 {
1148   ptid_t save_ptid;
1149   LONGEST retval;
1150
1151   if (ptid_equal (ptid, inferior_ptid))
1152     return read_signed_register (regnum);
1153
1154   save_ptid = inferior_ptid;
1155
1156   inferior_ptid = ptid;
1157
1158   retval = read_signed_register (regnum);
1159
1160   inferior_ptid = save_ptid;
1161
1162   return retval;
1163 }
1164
1165 /* Store VALUE into the raw contents of register number REGNUM.  */
1166
1167 void
1168 write_register (int regnum, LONGEST val)
1169 {
1170   void *buf;
1171   int size;
1172   size = REGISTER_RAW_SIZE (regnum);
1173   buf = alloca (size);
1174   store_signed_integer (buf, size, (LONGEST) val);
1175   deprecated_write_register_gen (regnum, buf);
1176 }
1177
1178 void
1179 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1180 {
1181   ptid_t save_ptid;
1182
1183   if (ptid_equal (ptid, inferior_ptid))
1184     {
1185       write_register (regnum, val);
1186       return;
1187     }
1188
1189   save_ptid = inferior_ptid;
1190
1191   inferior_ptid = ptid;
1192
1193   write_register (regnum, val);
1194
1195   inferior_ptid = save_ptid;
1196 }
1197
1198 /* SUPPLY_REGISTER()
1199
1200    Record that register REGNUM contains VAL.  This is used when the
1201    value is obtained from the inferior or core dump, so there is no
1202    need to store the value there.
1203
1204    If VAL is a NULL pointer, then it's probably an unsupported register.
1205    We just set its value to all zeros.  We might want to record this
1206    fact, and report it to the users of read_register and friends.  */
1207
1208 void
1209 supply_register (int regnum, const void *val)
1210 {
1211 #if 1
1212   if (! ptid_equal (registers_ptid, inferior_ptid))
1213     {
1214       registers_changed ();
1215       registers_ptid = inferior_ptid;
1216     }
1217 #endif
1218
1219   set_register_cached (regnum, 1);
1220   if (val)
1221     memcpy (register_buffer (current_regcache, regnum), val, 
1222             REGISTER_RAW_SIZE (regnum));
1223   else
1224     memset (register_buffer (current_regcache, regnum), '\000', 
1225             REGISTER_RAW_SIZE (regnum));
1226
1227   /* On some architectures, e.g. HPPA, there are a few stray bits in
1228      some registers, that the rest of the code would like to ignore.  */
1229
1230   /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1231      going to be deprecated.  Instead architectures will leave the raw
1232      register value as is and instead clean things up as they pass
1233      through the method gdbarch_pseudo_register_read() clean up the
1234      values. */
1235
1236 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1237   DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1238     (regnum, register_buffer (current_regcache, regnum));
1239 #endif
1240 }
1241
1242 void
1243 regcache_collect (int regnum, void *buf)
1244 {
1245   memcpy (buf, register_buffer (current_regcache, regnum),
1246           REGISTER_RAW_SIZE (regnum));
1247 }
1248
1249
1250 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc.  Special
1251    handling for registers PC, SP, and FP.  */
1252
1253 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1254    read_pc_pid(), read_pc(), generic_target_write_pc(),
1255    write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
1256    generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1257    read_fp(), will eventually be moved out of the reg-cache into
1258    either frame.[hc] or to the multi-arch framework.  The are not part
1259    of the raw register cache.  */
1260
1261 /* This routine is getting awfully cluttered with #if's.  It's probably
1262    time to turn this into READ_PC and define it in the tm.h file.
1263    Ditto for write_pc.
1264
1265    1999-06-08: The following were re-written so that it assumes the
1266    existence of a TARGET_READ_PC et.al. macro.  A default generic
1267    version of that macro is made available where needed.
1268
1269    Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1270    by the multi-arch framework, it will eventually be possible to
1271    eliminate the intermediate read_pc_pid().  The client would call
1272    TARGET_READ_PC directly. (cagney). */
1273
1274 CORE_ADDR
1275 generic_target_read_pc (ptid_t ptid)
1276 {
1277 #ifdef PC_REGNUM
1278   if (PC_REGNUM >= 0)
1279     {
1280       CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid));
1281       return pc_val;
1282     }
1283 #endif
1284   internal_error (__FILE__, __LINE__,
1285                   "generic_target_read_pc");
1286   return 0;
1287 }
1288
1289 CORE_ADDR
1290 read_pc_pid (ptid_t ptid)
1291 {
1292   ptid_t saved_inferior_ptid;
1293   CORE_ADDR pc_val;
1294
1295   /* In case ptid != inferior_ptid. */
1296   saved_inferior_ptid = inferior_ptid;
1297   inferior_ptid = ptid;
1298
1299   pc_val = TARGET_READ_PC (ptid);
1300
1301   inferior_ptid = saved_inferior_ptid;
1302   return pc_val;
1303 }
1304
1305 CORE_ADDR
1306 read_pc (void)
1307 {
1308   return read_pc_pid (inferior_ptid);
1309 }
1310
1311 void
1312 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1313 {
1314 #ifdef PC_REGNUM
1315   if (PC_REGNUM >= 0)
1316     write_register_pid (PC_REGNUM, pc, ptid);
1317   if (NPC_REGNUM >= 0)
1318     write_register_pid (NPC_REGNUM, pc + 4, ptid);
1319 #else
1320   internal_error (__FILE__, __LINE__,
1321                   "generic_target_write_pc");
1322 #endif
1323 }
1324
1325 void
1326 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1327 {
1328   ptid_t saved_inferior_ptid;
1329
1330   /* In case ptid != inferior_ptid. */
1331   saved_inferior_ptid = inferior_ptid;
1332   inferior_ptid = ptid;
1333
1334   TARGET_WRITE_PC (pc, ptid);
1335
1336   inferior_ptid = saved_inferior_ptid;
1337 }
1338
1339 void
1340 write_pc (CORE_ADDR pc)
1341 {
1342   write_pc_pid (pc, inferior_ptid);
1343 }
1344
1345 /* Cope with strage ways of getting to the stack and frame pointers */
1346
1347 CORE_ADDR
1348 generic_target_read_sp (void)
1349 {
1350 #ifdef SP_REGNUM
1351   if (SP_REGNUM >= 0)
1352     return read_register (SP_REGNUM);
1353 #endif
1354   internal_error (__FILE__, __LINE__,
1355                   "generic_target_read_sp");
1356 }
1357
1358 CORE_ADDR
1359 read_sp (void)
1360 {
1361   return TARGET_READ_SP ();
1362 }
1363
1364 void
1365 generic_target_write_sp (CORE_ADDR val)
1366 {
1367 #ifdef SP_REGNUM
1368   if (SP_REGNUM >= 0)
1369     {
1370       write_register (SP_REGNUM, val);
1371       return;
1372     }
1373 #endif
1374   internal_error (__FILE__, __LINE__,
1375                   "generic_target_write_sp");
1376 }
1377
1378 void
1379 write_sp (CORE_ADDR val)
1380 {
1381   TARGET_WRITE_SP (val);
1382 }
1383
1384 CORE_ADDR
1385 generic_target_read_fp (void)
1386 {
1387 #ifdef FP_REGNUM
1388   if (FP_REGNUM >= 0)
1389     return read_register (FP_REGNUM);
1390 #endif
1391   internal_error (__FILE__, __LINE__,
1392                   "generic_target_read_fp");
1393 }
1394
1395 CORE_ADDR
1396 read_fp (void)
1397 {
1398   return TARGET_READ_FP ();
1399 }
1400
1401 /* ARGSUSED */
1402 static void
1403 reg_flush_command (char *command, int from_tty)
1404 {
1405   /* Force-flush the register cache.  */
1406   registers_changed ();
1407   if (from_tty)
1408     printf_filtered ("Register cache flushed.\n");
1409 }
1410
1411 static void
1412 build_regcache (void)
1413 {
1414   current_regcache = regcache_xmalloc (current_gdbarch);
1415   current_regcache->readonly_p = 0;
1416   registers = deprecated_grub_regcache_for_registers (current_regcache);
1417   deprecated_register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1418 }
1419
1420 static void
1421 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1422                    const unsigned char *buf, long len)
1423 {
1424   int i;
1425   switch (endian)
1426     {
1427     case BFD_ENDIAN_BIG:
1428       for (i = 0; i < len; i++)
1429         fprintf_unfiltered (file, "%02x", buf[i]);
1430       break;
1431     case BFD_ENDIAN_LITTLE:
1432       for (i = len - 1; i >= 0; i--)
1433         fprintf_unfiltered (file, "%02x", buf[i]);
1434       break;
1435     default:
1436       internal_error (__FILE__, __LINE__, "Bad switch");
1437     }
1438 }
1439
1440 enum regcache_dump_what
1441 {
1442   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1443 };
1444
1445 static void
1446 regcache_dump (struct regcache *regcache, struct ui_file *file,
1447                enum regcache_dump_what what_to_dump)
1448 {
1449   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1450   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1451   struct reggroup *const *groups = reggroups (gdbarch);
1452   int regnum;
1453   int footnote_nr = 0;
1454   int footnote_register_size = 0;
1455   int footnote_register_offset = 0;
1456   int footnote_register_type_name_null = 0;
1457   long register_offset = 0;
1458   unsigned char *buf = alloca (regcache->descr->max_register_size);
1459
1460 #if 0
1461   fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1462   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1463                       regcache->descr->nr_raw_registers);
1464   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1465                       regcache->descr->nr_cooked_registers);
1466   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1467                       regcache->descr->sizeof_raw_registers);
1468   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1469                       regcache->descr->sizeof_raw_register_valid_p);
1470   fprintf_unfiltered (file, "max_register_size %ld\n",
1471                       regcache->descr->max_register_size);
1472   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1473   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1474 #endif
1475
1476   gdb_assert (regcache->descr->nr_cooked_registers
1477               == (NUM_REGS + NUM_PSEUDO_REGS));
1478
1479   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1480     {
1481       /* Name.  */
1482       if (regnum < 0)
1483         fprintf_unfiltered (file, " %-10s", "Name");
1484       else
1485         {
1486           const char *p = REGISTER_NAME (regnum);
1487           if (p == NULL)
1488             p = "";
1489           else if (p[0] == '\0')
1490             p = "''";
1491           fprintf_unfiltered (file, " %-10s", p);
1492         }
1493
1494       /* Number.  */
1495       if (regnum < 0)
1496         fprintf_unfiltered (file, " %4s", "Nr");
1497       else
1498         fprintf_unfiltered (file, " %4d", regnum);
1499
1500       /* Relative number.  */
1501       if (regnum < 0)
1502         fprintf_unfiltered (file, " %4s", "Rel");
1503       else if (regnum < NUM_REGS)
1504         fprintf_unfiltered (file, " %4d", regnum);
1505       else
1506         fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1507
1508       /* Offset.  */
1509       if (regnum < 0)
1510         fprintf_unfiltered (file, " %6s  ", "Offset");
1511       else
1512         {
1513           fprintf_unfiltered (file, " %6ld",
1514                               regcache->descr->register_offset[regnum]);
1515           if (register_offset != regcache->descr->register_offset[regnum]
1516               || register_offset != REGISTER_BYTE (regnum)
1517               || (regnum > 0
1518                   && (regcache->descr->register_offset[regnum]
1519                       != (regcache->descr->register_offset[regnum - 1]
1520                           + regcache->descr->sizeof_register[regnum - 1])))
1521               )
1522             {
1523               if (!footnote_register_offset)
1524                 footnote_register_offset = ++footnote_nr;
1525               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1526             }
1527           else
1528             fprintf_unfiltered (file, "  ");
1529           register_offset = (regcache->descr->register_offset[regnum]
1530                              + regcache->descr->sizeof_register[regnum]);
1531         }
1532
1533       /* Size.  */
1534       if (regnum < 0)
1535         fprintf_unfiltered (file, " %5s ", "Size");
1536       else
1537         {
1538           fprintf_unfiltered (file, " %5ld",
1539                               regcache->descr->sizeof_register[regnum]);
1540           if ((regcache->descr->sizeof_register[regnum]
1541                != REGISTER_RAW_SIZE (regnum))
1542               || (regcache->descr->sizeof_register[regnum]
1543                   != REGISTER_VIRTUAL_SIZE (regnum))
1544               || (regcache->descr->sizeof_register[regnum]
1545                   != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1546                                                  regnum)))
1547               )
1548             {
1549               if (!footnote_register_size)
1550                 footnote_register_size = ++footnote_nr;
1551               fprintf_unfiltered (file, "*%d", footnote_register_size);
1552             }
1553           else
1554             fprintf_unfiltered (file, " ");
1555         }
1556
1557       /* Type.  */
1558       {
1559         const char *t;
1560         if (regnum < 0)
1561           t = "Type";
1562         else
1563           {
1564             static const char blt[] = "builtin_type";
1565             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1566             if (t == NULL)
1567               {
1568                 char *n;
1569                 if (!footnote_register_type_name_null)
1570                   footnote_register_type_name_null = ++footnote_nr;
1571                 xasprintf (&n, "*%d", footnote_register_type_name_null);
1572                 make_cleanup (xfree, n);
1573                 t = n;
1574               }
1575             /* Chop a leading builtin_type.  */
1576             if (strncmp (t, blt, strlen (blt)) == 0)
1577               t += strlen (blt);
1578           }
1579         fprintf_unfiltered (file, " %-15s", t);
1580       }
1581
1582       /* Leading space always present.  */
1583       fprintf_unfiltered (file, " ");
1584
1585       /* Value, raw.  */
1586       if (what_to_dump == regcache_dump_raw)
1587         {
1588           if (regnum < 0)
1589             fprintf_unfiltered (file, "Raw value");
1590           else if (regnum >= regcache->descr->nr_raw_registers)
1591             fprintf_unfiltered (file, "<cooked>");
1592           else if (!regcache_valid_p (regcache, regnum))
1593             fprintf_unfiltered (file, "<invalid>");
1594           else
1595             {
1596               regcache_raw_read (regcache, regnum, buf);
1597               fprintf_unfiltered (file, "0x");
1598               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1599                                  REGISTER_RAW_SIZE (regnum));
1600             }
1601         }
1602
1603       /* Value, cooked.  */
1604       if (what_to_dump == regcache_dump_cooked)
1605         {
1606           if (regnum < 0)
1607             fprintf_unfiltered (file, "Cooked value");
1608           else
1609             {
1610               regcache_cooked_read (regcache, regnum, buf);
1611               fprintf_unfiltered (file, "0x");
1612               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1613                                  REGISTER_VIRTUAL_SIZE (regnum));
1614             }
1615         }
1616
1617       /* Group members.  */
1618       if (what_to_dump == regcache_dump_groups)
1619         {
1620           if (regnum < 0)
1621             fprintf_unfiltered (file, "Groups");
1622           else
1623             {
1624               int i;
1625               const char *sep = "";
1626               for (i = 0; groups[i] != NULL; i++)
1627                 {
1628                   if (gdbarch_register_reggroup_p (gdbarch, regnum, groups[i]))
1629                     {
1630                       fprintf_unfiltered (file, "%s%s", sep, reggroup_name (groups[i]));
1631                       sep = ",";
1632                     }
1633                 }
1634             }
1635         }
1636
1637       fprintf_unfiltered (file, "\n");
1638     }
1639
1640   if (footnote_register_size)
1641     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1642                         footnote_register_size);
1643   if (footnote_register_offset)
1644     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1645                         footnote_register_offset);
1646   if (footnote_register_type_name_null)
1647     fprintf_unfiltered (file, 
1648                         "*%d: Register type's name NULL.\n",
1649                         footnote_register_type_name_null);
1650   do_cleanups (cleanups);
1651 }
1652
1653 static void
1654 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1655 {
1656   if (args == NULL)
1657     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1658   else
1659     {
1660       struct ui_file *file = gdb_fopen (args, "w");
1661       if (file == NULL)
1662         perror_with_name ("maintenance print architecture");
1663       regcache_dump (current_regcache, file, what_to_dump);    
1664       ui_file_delete (file);
1665     }
1666 }
1667
1668 static void
1669 maintenance_print_registers (char *args, int from_tty)
1670 {
1671   regcache_print (args, regcache_dump_none);
1672 }
1673
1674 static void
1675 maintenance_print_raw_registers (char *args, int from_tty)
1676 {
1677   regcache_print (args, regcache_dump_raw);
1678 }
1679
1680 static void
1681 maintenance_print_cooked_registers (char *args, int from_tty)
1682 {
1683   regcache_print (args, regcache_dump_cooked);
1684 }
1685
1686 static void
1687 maintenance_print_register_groups (char *args, int from_tty)
1688 {
1689   regcache_print (args, regcache_dump_groups);
1690 }
1691
1692 void
1693 _initialize_regcache (void)
1694 {
1695   regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1696                                                  xfree_regcache_descr);
1697   REGISTER_GDBARCH_SWAP (current_regcache);
1698   register_gdbarch_swap (&registers, sizeof (registers), NULL);
1699   register_gdbarch_swap (&deprecated_register_valid, sizeof (deprecated_register_valid), NULL);
1700   register_gdbarch_swap (NULL, 0, build_regcache);
1701
1702   add_com ("flushregs", class_maintenance, reg_flush_command,
1703            "Force gdb to flush its register cache (maintainer command)");
1704
1705    /* Initialize the thread/process associated with the current set of
1706       registers.  For now, -1 is special, and means `no current process'.  */
1707   registers_ptid = pid_to_ptid (-1);
1708
1709   add_cmd ("registers", class_maintenance,
1710            maintenance_print_registers,
1711            "Print the internal register configuration.\
1712 Takes an optional file parameter.",
1713            &maintenanceprintlist);
1714   add_cmd ("raw-registers", class_maintenance,
1715            maintenance_print_raw_registers,
1716            "Print the internal register configuration including raw values.\
1717 Takes an optional file parameter.",
1718            &maintenanceprintlist);
1719   add_cmd ("cooked-registers", class_maintenance,
1720            maintenance_print_cooked_registers,
1721            "Print the internal register configuration including cooked values.\
1722 Takes an optional file parameter.",
1723            &maintenanceprintlist);
1724   add_cmd ("register-groups", class_maintenance,
1725            maintenance_print_register_groups,
1726            "Print the internal register configuration including each register's group.\
1727 Takes an optional file parameter.",
1728            &maintenanceprintlist);
1729
1730 }