* Fixing typo that caused infinite loop upon PKE MPG.
[binutils-gdb.git] / gdb / mn10300-tdep.c
blob616abb7960dd48714a722c8c294f770a283c80df
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2 Copyright 1996, 1997 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
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"
31 /* Info gleaned from scanning a function's prologue. */
33 struct pifsr /* Info about one saved reg */
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int reg; /* Saved register number */
40 struct prologue_info
42 int framereg;
43 int frameoffset;
44 int start_function;
45 struct pifsr *pifsrs;
48 /* Function: frame_chain
49 Figure out and return the caller's frame pointer given current
50 frame_info struct.
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.
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?
61 CORE_ADDR
62 mn10300_frame_chain (fi)
63 struct frame_info *fi;
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;
71 struct pifsr *pifsr, *pifsr_tmp;
73 /* current pc is fi->pc */
74 /* current fp is fi->frame */
76 /* current sp is: */
77 curr_sp = read_register (SP_REGNUM);
80 printf("curr pc = 0x%x ; curr fp = 0x%x ; curr sp = 0x%x\n",
81 fi->pc, fi->frame, curr_sp);
84 /* first inst after prologue is: */
85 past_prologue_addr = mn10300_skip_prologue (fi->pc);
87 /* Are we in the prologue? */
88 /* Yes if mn10300_skip_prologue returns an address after the
89 current pc in which case we have to scan prologue */
90 if (fi->pc < mn10300_skip_prologue (fi->pc))
91 past_prologue = 0;
93 /* scan prologue if we're not past it */
94 if (!past_prologue)
96 /* printf("scanning prologue\n"); */
97 /* FIXME -- fill out this case later */
98 return 0x0; /* bogus value */
101 if (past_prologue) /* if we don't need to scan the prologue */
103 /* printf("we're past the prologue\n"); */
104 callers_pc = fi->frame - REGISTER_SIZE;
105 callers_fp = fi->frame - ((n_movm_args + 1) * REGISTER_SIZE);
107 printf("callers_pc = 0x%x ; callers_fp = 0x%x\n",
108 callers_pc, callers_fp);
110 printf("*callers_pc = 0x%x ; *callers_fp = 0x%x\n",
111 read_memory_integer(callers_pc, REGISTER_SIZE),
112 read_memory_integer(callers_fp, REGISTER_SIZE));
114 return read_memory_integer(callers_fp, REGISTER_SIZE);
117 /* we don't get here */
120 /* Function: find_callers_reg
121 Find REGNUM on the stack. Otherwise, it's in an active register.
122 One thing we might want to do here is to check REGNUM against the
123 clobber mask, and somehow flag it as invalid if it isn't saved on
124 the stack somewhere. This would provide a graceful failure mode
125 when trying to get the value of caller-saves registers for an inner
126 frame. */
128 CORE_ADDR
129 mn10300_find_callers_reg (fi, regnum)
130 struct frame_info *fi;
131 int regnum;
133 /* printf("mn10300_find_callers_reg\n"); */
135 for (; fi; fi = fi->next)
136 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
137 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
138 else if (fi->fsr.regs[regnum] != 0)
139 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
140 REGISTER_RAW_SIZE(regnum));
142 return read_register (regnum);
145 /* Function: skip_prologue
146 Return the address of the first inst past the prologue of the function.
149 CORE_ADDR
150 mn10300_skip_prologue (pc)
151 CORE_ADDR pc;
153 CORE_ADDR func_addr, func_end;
155 /* printf("mn10300_skip_prologue\n"); */
157 /* See what the symbol table says */
159 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
161 struct symtab_and_line sal;
163 sal = find_pc_line (func_addr, 0);
165 if (sal.line != 0 && sal.end < func_end)
166 return sal.end;
167 else
168 /* Either there's no line info, or the line after the prologue is after
169 the end of the function. In this case, there probably isn't a
170 prologue. */
171 return pc;
174 /* We can't find the start of this function, so there's nothing we can do. */
175 return pc;
178 /* Function: pop_frame
179 This routine gets called when either the user uses the `return'
180 command, or the call dummy breakpoint gets hit. */
182 void
183 mn10300_pop_frame (frame)
184 struct frame_info *frame;
186 int regnum;
188 /* printf("mn10300_pop_frame start\n"); */
190 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
191 generic_pop_dummy_frame ();
192 else
194 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
196 for (regnum = 0; regnum < NUM_REGS; regnum++)
197 if (frame->fsr.regs[regnum] != 0)
198 write_register (regnum,
199 read_memory_unsigned_integer (frame->fsr.regs[regnum],
200 REGISTER_RAW_SIZE(regnum)));
202 write_register (SP_REGNUM, FRAME_FP (frame));
205 flush_cached_frames ();
207 /* printf("mn10300_pop_frame end\n"); */
210 /* Function: push_arguments
211 Setup arguments for a call to the target. Arguments go in
212 order on the stack.
215 CORE_ADDR
216 mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr)
217 int nargs;
218 value_ptr *args;
219 CORE_ADDR sp;
220 unsigned char struct_return;
221 CORE_ADDR struct_addr;
223 int argnum = 0;
224 int len = 0;
225 int stack_offset = 0; /* copy args to this offset onto stack */
227 /* printf("mn10300_push_arguments start\n"); */
229 /* First, just for safety, make sure stack is aligned */
230 sp &= ~3;
232 /* Now make space on the stack for the args. */
233 for (argnum = 0; argnum < nargs; argnum++)
234 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
236 sp -= len;
238 /* Push all arguments onto the stack. */
239 for (argnum = 0; argnum < nargs; argnum++)
241 int len;
242 char *val;
244 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
245 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
247 /* for now, pretend structs aren't special */
248 len = TYPE_LENGTH (VALUE_TYPE (*args));
249 val = (char *)VALUE_CONTENTS (*args);
251 else
253 len = TYPE_LENGTH (VALUE_TYPE (*args));
254 val = (char *)VALUE_CONTENTS (*args);
257 while (len > 0)
259 write_memory (sp + stack_offset, val, 4);
261 len -= 4;
262 val += 4;
263 stack_offset += 4;
265 args++;
268 /* printf"mn10300_push_arguments end\n"); */
270 return sp;
273 /* Function: push_return_address (pc)
274 Set up the return address for the inferior function call.
275 Needed for targets where we don't actually execute a JSR/BSR instruction */
277 CORE_ADDR
278 mn10300_push_return_address (pc, sp)
279 CORE_ADDR pc;
280 CORE_ADDR sp;
282 /* printf("mn10300_push_return_address\n"); */
284 /* write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ()); */
285 return sp;
288 /* Function: frame_saved_pc
289 Find the caller of this frame. We do this by seeing if RP_REGNUM
290 is saved in the stack anywhere, otherwise we get it from the
291 registers. If the inner frame is a dummy frame, return its PC
292 instead of RP, because that's where "caller" of the dummy-frame
293 will be found. */
295 CORE_ADDR
296 mn10300_frame_saved_pc (fi)
297 struct frame_info *fi;
299 /* printf("mn10300_frame_saved_pc\n"); */
301 return (read_memory_integer(fi->frame - REGISTER_SIZE, REGISTER_SIZE));
304 void
305 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
306 char *raw_buffer;
307 int *optimized;
308 CORE_ADDR *addrp;
309 struct frame_info *frame;
310 int regnum;
311 enum lval_type *lval;
313 /* printf("get_saved_register\n"); */
315 generic_get_saved_register (raw_buffer, optimized, addrp,
316 frame, regnum, lval);
319 /* Function: init_extra_frame_info
320 Setup the frame's frame pointer, pc, and frame addresses for saved
321 registers. Most of the work is done in frame_chain().
323 Note that when we are called for the last frame (currently active frame),
324 that fi->pc and fi->frame will already be setup. However, fi->frame will
325 be valid only if this routine uses FP. For previous frames, fi-frame will
326 always be correct (since that is derived from v850_frame_chain ()).
328 We can be called with the PC in the call dummy under two circumstances.
329 First, during normal backtracing, second, while figuring out the frame
330 pointer just prior to calling the target function (see run_stack_dummy).
333 void
334 mn10300_init_extra_frame_info (fi)
335 struct frame_info *fi;
337 struct prologue_info pi;
338 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
339 int reg;
341 if (fi->next)
342 fi->pc = FRAME_SAVED_PC (fi->next);
344 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
346 /* The call dummy doesn't save any registers on the stack, so we can return
347 now. */
349 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
350 return;
352 pi.pifsrs = pifsrs;
355 /* v850_scan_prologue (fi->pc, &pi); */
357 if (!fi->next && pi.framereg == SP_REGNUM)
358 fi->frame = read_register (pi.framereg) - pi.frameoffset;
360 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
362 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
364 if (pifsr->framereg == SP_REGNUM)
365 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
368 /* printf("init_extra_frame_info\n"); */
371 void
372 _initialize_mn10300_tdep ()
374 /* printf("_initialize_mn10300_tdep\n"); */
376 tm_print_insn = print_insn_mn10300;