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