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