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