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