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