Tue Jan 28 15:39:50 1997 Geoffrey Noer <noer@cygnus.com>
[binutils-gdb.git] / gdb / mn10200-tdep.c
1 /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2 Copyright 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Info gleaned from scanning a function's prologue. */
32
33 struct pifsr /* Info about one saved reg */
34 {
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int reg; /* Saved register number */
38 };
39
40 struct prologue_info
41 {
42 int framereg;
43 int frameoffset;
44 int start_function;
45 struct pifsr *pifsrs;
46 };
47
48 /* Function: frame_chain
49 Figure out and return the caller's frame pointer given current
50 frame_info struct.
51
52 We start out knowing the current pc, current sp, current fp.
53 We want to determine the caller's fp and caller's pc. To do this
54 correctly, we have to be able to handle the case where we are in the
55 middle of the prologue which involves scanning the prologue.
56
57 We don't handle dummy frames yet but we would probably just return the
58 stack pointer that was in use at the time the function call was made?
59 */
60
61 CORE_ADDR
62 mn10200_frame_chain (fi)
63 struct frame_info *fi;
64 {
65 struct prologue_info pi;
66 CORE_ADDR callers_pc, callers_fp, curr_sp;
67 CORE_ADDR past_prologue_addr;
68 int past_prologue = 1; /* default to being past prologue */
69 int n_movm_args = 4;
70
71 struct pifsr *pifsr, *pifsr_tmp;
72
73 /* current pc is fi->pc */
74 /* current fp is fi->frame */
75 /* current sp is: */
76 curr_sp = read_register (SP_REGNUM);
77
78 /*
79 printf("curr pc = 0x%x ; curr fp = 0x%x ; curr sp = 0x%x\n",
80 fi->pc, fi->frame, curr_sp);
81 */
82
83 /* first inst after prologue is: */
84 past_prologue_addr = mn10200_skip_prologue (fi->pc);
85
86 /* Are we in the prologue? */
87 /* Yes if mn10200_skip_prologue returns an address after the
88 current pc in which case we have to scan prologue */
89 if (fi->pc < mn10200_skip_prologue (fi->pc))
90 past_prologue = 0;
91
92 /* scan prologue if we're not past it */
93 if (!past_prologue)
94 {
95 /* printf("scanning prologue\n"); */
96 /* FIXME -- fill out this case later */
97 return 0x0; /* bogus value */
98 }
99
100 if (past_prologue) /* if we don't need to scan the prologue */
101 {
102 callers_pc = fi->frame - REGISTER_SIZE;
103 callers_fp = fi->frame - (4 * REGISTER_SIZE);
104
105 #if 0
106 printf("callers_pc = 0x%x ; callers_fp = 0x%x\n",
107 callers_pc, callers_fp);
108 printf("*callers_pc = 0x%x ; *callers_fp = 0x%x\n",
109 read_memory_integer(callers_pc, REGISTER_SIZE),
110 read_memory_integer(callers_fp, REGISTER_SIZE));
111 #endif
112
113 return read_memory_integer(callers_fp, REGISTER_SIZE);
114 }
115
116 /* we don't get here */
117 }
118
119 /* Function: find_callers_reg
120 Find REGNUM on the stack. Otherwise, it's in an active register.
121 One thing we might want to do here is to check REGNUM against the
122 clobber mask, and somehow flag it as invalid if it isn't saved on
123 the stack somewhere. This would provide a graceful failure mode
124 when trying to get the value of caller-saves registers for an inner
125 frame. */
126
127 CORE_ADDR
128 mn10200_find_callers_reg (fi, regnum)
129 struct frame_info *fi;
130 int regnum;
131 {
132 /* printf("mn10200_find_callers_reg\n"); */
133
134 for (; fi; fi = fi->next)
135 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
136 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
137 else if (fi->fsr.regs[regnum] != 0)
138 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
139 REGISTER_RAW_SIZE(regnum));
140
141 return read_register (regnum);
142 }
143
144 /* Function: skip_prologue
145 Return the address of the first inst past the prologue of the function.
146 */
147
148 CORE_ADDR
149 mn10200_skip_prologue (pc)
150 CORE_ADDR pc;
151 {
152 CORE_ADDR func_addr, func_end;
153
154 /* printf("mn10200_skip_prologue\n"); */
155
156 /* See what the symbol table says */
157
158 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
159 {
160 struct symtab_and_line sal;
161
162 sal = find_pc_line (func_addr, 0);
163
164 if (sal.line != 0 && sal.end < func_end)
165 return sal.end;
166 else
167 /* Either there's no line info, or the line after the prologue is after
168 the end of the function. In this case, there probably isn't a
169 prologue. */
170 return pc;
171 }
172
173 /* We can't find the start of this function, so there's nothing we can do. */
174 return pc;
175 }
176
177 /* Function: pop_frame
178 This routine gets called when either the user uses the `return'
179 command, or the call dummy breakpoint gets hit. */
180
181 void
182 mn10200_pop_frame (frame)
183 struct frame_info *frame;
184 {
185 int regnum;
186
187 /* printf("mn10200_pop_frame start\n"); */
188
189 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
190 generic_pop_dummy_frame ();
191 else
192 {
193 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
194
195 for (regnum = 0; regnum < NUM_REGS; regnum++)
196 if (frame->fsr.regs[regnum] != 0)
197 write_register (regnum,
198 read_memory_unsigned_integer (frame->fsr.regs[regnum],
199 REGISTER_RAW_SIZE(regnum)));
200
201 write_register (SP_REGNUM, FRAME_FP (frame));
202 }
203
204 flush_cached_frames ();
205
206 /* printf("mn10200_pop_frame end\n"); */
207 }
208
209 /* Function: push_arguments
210 Setup arguments for a call to the target. Arguments go in
211 order on the stack.
212 */
213
214 CORE_ADDR
215 mn10200_push_arguments (nargs, args, sp, struct_return, struct_addr)
216 int nargs;
217 value_ptr *args;
218 CORE_ADDR sp;
219 unsigned char struct_return;
220 CORE_ADDR struct_addr;
221 {
222 int argnum = 0;
223 int len = 0;
224 int stack_offset = 0; /* copy args to this offset onto stack */
225
226 /* printf("mn10200_push_arguments start\n"); */
227
228 /* First, just for safety, make sure stack is aligned */
229 sp &= ~3;
230
231 /* Now make space on the stack for the args. */
232 for (argnum = 0; argnum < nargs; argnum++)
233 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
234
235 sp -= len;
236
237 /* Push all arguments onto the stack. */
238 for (argnum = 0; argnum < nargs; argnum++)
239 {
240 int len;
241 char *val;
242
243 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
244 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
245 {
246 /* for now, pretend structs aren't special */
247 len = TYPE_LENGTH (VALUE_TYPE (*args));
248 val = (char *)VALUE_CONTENTS (*args);
249 }
250 else
251 {
252 len = TYPE_LENGTH (VALUE_TYPE (*args));
253 val = (char *)VALUE_CONTENTS (*args);
254 }
255
256 while (len > 0)
257 {
258 write_memory (sp + stack_offset, val, 4);
259
260 len -= 4;
261 val += 4;
262 stack_offset += 4;
263 }
264 args++;
265 }
266
267 /* printf"mn10200_push_arguments end\n"); */
268
269 return sp;
270 }
271
272 /* Function: push_return_address (pc)
273 Set up the return address for the inferior function call.
274 Needed for targets where we don't actually execute a JSR/BSR instruction */
275
276 CORE_ADDR
277 mn10200_push_return_address (pc, sp)
278 CORE_ADDR pc;
279 CORE_ADDR sp;
280 {
281 /* printf("mn10200_push_return_address\n"); */
282
283 /* write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ()); */
284 return sp;
285 }
286
287 /* Function: frame_saved_pc
288 Find the caller of this frame. We do this by seeing if RP_REGNUM
289 is saved in the stack anywhere, otherwise we get it from the
290 registers. If the inner frame is a dummy frame, return its PC
291 instead of RP, because that's where "caller" of the dummy-frame
292 will be found. */
293
294 CORE_ADDR
295 mn10200_frame_saved_pc (fi)
296 struct frame_info *fi;
297 {
298 /* printf("mn10200_frame_saved_pc\n"); */
299
300 return (read_memory_integer(fi->frame - REGISTER_SIZE, REGISTER_SIZE));
301 }
302
303 void
304 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
305 char *raw_buffer;
306 int *optimized;
307 CORE_ADDR *addrp;
308 struct frame_info *frame;
309 int regnum;
310 enum lval_type *lval;
311 {
312 /* printf("get_saved_register\n"); */
313
314 generic_get_saved_register (raw_buffer, optimized, addrp,
315 frame, regnum, lval);
316 }
317
318 /* Function: init_extra_frame_info
319 Setup the frame's frame pointer, pc, and frame addresses for saved
320 registers. Most of the work is done in frame_chain().
321
322 Note that when we are called for the last frame (currently active frame),
323 that fi->pc and fi->frame will already be setup. However, fi->frame will
324 be valid only if this routine uses FP. For previous frames, fi-frame will
325 always be correct (since that is derived from v850_frame_chain ()).
326
327 We can be called with the PC in the call dummy under two circumstances.
328 First, during normal backtracing, second, while figuring out the frame
329 pointer just prior to calling the target function (see run_stack_dummy).
330 */
331
332 void
333 mn10200_init_extra_frame_info (fi)
334 struct frame_info *fi;
335 {
336 struct prologue_info pi;
337 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
338 int reg;
339
340 if (fi->next)
341 fi->pc = FRAME_SAVED_PC (fi->next);
342
343 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
344
345 /* The call dummy doesn't save any registers on the stack, so we can return
346 now. */
347 /*
348 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
349 return;
350
351 pi.pifsrs = pifsrs;
352 */
353
354 /* v850_scan_prologue (fi->pc, &pi); */
355 /*
356 if (!fi->next && pi.framereg == SP_REGNUM)
357 fi->frame = read_register (pi.framereg) - pi.frameoffset;
358
359 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
360 {
361 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
362
363 if (pifsr->framereg == SP_REGNUM)
364 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
365 }
366 */
367 /* printf("init_extra_frame_info\n"); */
368 }
369
370 void
371 _initialize_mn10200_tdep ()
372 {
373 /* printf("_initialize_mn10200_tdep\n"); */
374
375 tm_print_insn = print_insn_mn10200;
376 }
377