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