Move frame specific register code to the new file frame.c.
[external/binutils.git] / gdb / frame.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
3    2001 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "target.h"
25 #include "value.h"
26 #include "inferior.h"   /* for inferior_pid */
27
28 /* FIND_SAVED_REGISTER ()
29
30    Return the address in which frame FRAME's value of register REGNUM
31    has been saved in memory.  Or return zero if it has not been saved.
32    If REGNUM specifies the SP, the value we return is actually
33    the SP value, not an address where it was saved.  */
34
35 CORE_ADDR
36 find_saved_register (struct frame_info *frame, int regnum)
37 {
38   register struct frame_info *frame1 = NULL;
39   register CORE_ADDR addr = 0;
40
41   if (frame == NULL)            /* No regs saved if want current frame */
42     return 0;
43
44 #ifdef HAVE_REGISTER_WINDOWS
45   /* We assume that a register in a register window will only be saved
46      in one place (since the name changes and/or disappears as you go
47      towards inner frames), so we only call get_frame_saved_regs on
48      the current frame.  This is directly in contradiction to the
49      usage below, which assumes that registers used in a frame must be
50      saved in a lower (more interior) frame.  This change is a result
51      of working on a register window machine; get_frame_saved_regs
52      always returns the registers saved within a frame, within the
53      context (register namespace) of that frame. */
54
55   /* However, note that we don't want this to return anything if
56      nothing is saved (if there's a frame inside of this one).  Also,
57      callers to this routine asking for the stack pointer want the
58      stack pointer saved for *this* frame; this is returned from the
59      next frame.  */
60
61   if (REGISTER_IN_WINDOW_P (regnum))
62     {
63       frame1 = get_next_frame (frame);
64       if (!frame1)
65         return 0;               /* Registers of this frame are active.  */
66
67       /* Get the SP from the next frame in; it will be this
68          current frame.  */
69       if (regnum != SP_REGNUM)
70         frame1 = frame;
71
72       FRAME_INIT_SAVED_REGS (frame1);
73       return frame1->saved_regs[regnum];        /* ... which might be zero */
74     }
75 #endif /* HAVE_REGISTER_WINDOWS */
76
77   /* Note that this next routine assumes that registers used in
78      frame x will be saved only in the frame that x calls and
79      frames interior to it.  This is not true on the sparc, but the
80      above macro takes care of it, so we should be all right. */
81   while (1)
82     {
83       QUIT;
84       frame1 = get_prev_frame (frame1);
85       if (frame1 == 0 || frame1 == frame)
86         break;
87       FRAME_INIT_SAVED_REGS (frame1);
88       if (frame1->saved_regs[regnum])
89         addr = frame1->saved_regs[regnum];
90     }
91
92   return addr;
93 }
94
95 /* DEFAULT_GET_SAVED_REGISTER ()
96
97    Find register number REGNUM relative to FRAME and put its (raw,
98    target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
99    variable was optimized out (and thus can't be fetched).  Set *LVAL
100    to lval_memory, lval_register, or not_lval, depending on whether
101    the value was fetched from memory, from a register, or in a strange
102    and non-modifiable way (e.g. a frame pointer which was calculated
103    rather than fetched).  Set *ADDRP to the address, either in memory
104    on as a REGISTER_BYTE offset into the registers array.
105
106    Note that this implementation never sets *LVAL to not_lval.  But
107    it can be replaced by defining GET_SAVED_REGISTER and supplying
108    your own.
109
110    The argument RAW_BUFFER must point to aligned memory.  */
111
112 static void
113 default_get_saved_register (char *raw_buffer,
114                             int *optimized,
115                             CORE_ADDR *addrp,
116                             struct frame_info *frame,
117                             int regnum,
118                             enum lval_type *lval)
119 {
120   CORE_ADDR addr;
121
122   if (!target_has_registers)
123     error ("No registers.");
124
125   /* Normal systems don't optimize out things with register numbers.  */
126   if (optimized != NULL)
127     *optimized = 0;
128   addr = find_saved_register (frame, regnum);
129   if (addr != 0)
130     {
131       if (lval != NULL)
132         *lval = lval_memory;
133       if (regnum == SP_REGNUM)
134         {
135           if (raw_buffer != NULL)
136             {
137               /* Put it back in target format.  */
138               store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
139                              (LONGEST) addr);
140             }
141           if (addrp != NULL)
142             *addrp = 0;
143           return;
144         }
145       if (raw_buffer != NULL)
146         target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
147     }
148   else
149     {
150       if (lval != NULL)
151         *lval = lval_register;
152       addr = REGISTER_BYTE (regnum);
153       if (raw_buffer != NULL)
154         read_register_gen (regnum, raw_buffer);
155     }
156   if (addrp != NULL)
157     *addrp = addr;
158 }
159
160 #if !defined (GET_SAVED_REGISTER)
161 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
162   default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
163 #endif
164
165 void
166 get_saved_register (char *raw_buffer,
167                     int *optimized,
168                     CORE_ADDR *addrp,
169                     struct frame_info *frame,
170                     int regnum,
171                     enum lval_type *lval)
172 {
173   GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
174 }
175
176 /* READ_RELATIVE_REGISTER_RAW_BYTES_FOR_FRAME
177
178    Copy the bytes of register REGNUM, relative to the input stack frame,
179    into our memory at MYADDR, in target byte order.
180    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
181
182    Returns 1 if could not be read, 0 if could.  */
183
184 /* FIXME: This function increases the confusion between FP_REGNUM
185    and the virtual/pseudo-frame pointer.  */
186
187 static int
188 read_relative_register_raw_bytes_for_frame (int regnum,
189                                             char *myaddr,
190                                             struct frame_info *frame)
191 {
192   int optim;
193   if (regnum == FP_REGNUM && frame)
194     {
195       /* Put it back in target format. */
196       store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
197                      (LONGEST) FRAME_FP (frame));
198
199       return 0;
200     }
201
202   get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
203                       regnum, (enum lval_type *) NULL);
204
205   if (register_cached (regnum) < 0)
206     return 1;                   /* register value not available */
207
208   return optim;
209 }
210
211 /* READ_RELATIVE_REGISTER_RAW_BYTES
212
213    Copy the bytes of register REGNUM, relative to the current stack
214    frame, into our memory at MYADDR, in target byte order.  
215    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
216
217    Returns 1 if could not be read, 0 if could.  */
218
219 int
220 read_relative_register_raw_bytes (int regnum, char *myaddr)
221 {
222   return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
223                                                      selected_frame);
224 }