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