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. */
27 #include "gdb_string.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 */
48 static CORE_ADDR mn10300_scan_prologue PARAMS ((CORE_ADDR pc,
49 struct prologue_info *fs));
51 /* Function: scan_prologue
52 Scan the prologue of the function that contains PC, and record what
53 we find in PI. PI->fsr must be zeroed by the called. Returns the
54 pc after the prologue. Note that the addresses saved in pi->fsr
55 are actually just frame relative (negative offsets from the frame
56 pointer). This is because we don't know the actual value of the
57 frame pointer yet. In some circumstances, the frame pointer can't
58 be determined till after we have scanned the prologue. */
61 mn10300_scan_prologue (pc, pi)
63 struct prologue_info *pi;
65 CORE_ADDR func_addr, prologue_end, current_pc;
69 printf("mn10300_scan_prologue start\n");
71 /* First, figure out the bounds of the prologue so that we can limit the
72 search to something reasonable. */
74 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
76 struct symtab_and_line sal;
78 sal = find_pc_line (func_addr, 0);
80 if (func_addr == entry_point_address ())
81 pi->start_function = 1;
83 pi->start_function = 0;
89 prologue_end = sal.end;
95 { /* We're in the boondocks */
100 prologue_end = min (prologue_end, pc);
102 /* Now, search the prologue looking for instructions that setup fp, save
103 rp, adjust sp and such. We also record the frame offset of any saved
107 pi->framereg = SP_REGNUM;
111 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
115 insn = read_memory_unsigned_integer (current_pc, 2);
117 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
118 || (insn & 0xffe0) == 0x0060 /* jmp */
119 || (insn & 0x0780) == 0x0580) /* branch */
120 break; /* Ran into end of prologue */
121 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
122 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
123 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
124 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
125 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
128 pi->framereg = FP_REGNUM;
130 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
132 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
135 pifsr->framereg = insn & 0x1f;
136 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
138 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
143 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
148 pifsr->framereg = 0; /* Tie off last entry */
150 printf("mn10300_scan_prologue end \n");
155 /* Function: init_extra_frame_info
156 Setup the frame's frame pointer, pc, and frame addresses for saved
157 registers. Most of the work is done in scan_prologue().
159 Note that when we are called for the last frame (currently active frame),
160 that fi->pc and fi->frame will already be setup. However, fi->frame will
161 be valid only if this routine uses FP. For previous frames, fi-frame will
162 always be correct (since that is derived from mn10300_frame_chain ()).
164 We can be called with the PC in the call dummy under two circumstances.
165 First, during normal backtracing, second, while figuring out the frame
166 pointer just prior to calling the target function (see run_stack_dummy). */
169 mn10300_init_extra_frame_info (fi)
170 struct frame_info *fi;
172 struct prologue_info pi;
173 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
176 printf("mn10300_init_extra_frame_info start\n");
179 fi->pc = FRAME_SAVED_PC (fi->next);
181 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
183 /* The call dummy doesn't save any registers on the stack, so we can return
185 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
190 mn10300_scan_prologue (fi->pc, &pi);
192 if (!fi->next && pi.framereg == SP_REGNUM)
193 fi->frame = read_register (pi.framereg) - pi.frameoffset;
195 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
197 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
199 if (pifsr->framereg == SP_REGNUM)
200 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
203 printf("mn10300_init_extra_frame_info end\n");
206 /* Function: frame_chain
207 Figure out the frame prior to FI. Unfortunately, this involves
208 scanning the prologue of the caller, which will also be done
209 shortly by mn10300_init_extra_frame_info. For the dummy frame, we
210 just return the stack pointer that was in use at the time the
211 function call was made. */
214 mn10300_frame_chain (fi)
215 struct frame_info *fi;
217 struct prologue_info pi;
218 CORE_ADDR callers_pc, fp;
220 printf("mn10300_frame_chain start\n");
222 /* First, find out who called us */
223 callers_pc = FRAME_SAVED_PC (fi);
224 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
225 fp = mn10300_find_callers_reg (fi, FP_REGNUM);
226 if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
227 return fp; /* caller is call-dummy: return oldest value of FP */
229 /* Caller is NOT a call-dummy, so everything else should just work.
230 Even if THIS frame is a call-dummy! */
233 mn10300_scan_prologue (callers_pc, &pi);
235 printf("mn10300_frame_chain end\n");
237 if (pi.start_function)
238 return 0; /* Don't chain beyond the start function */
240 if (pi.framereg == FP_REGNUM)
241 return mn10300_find_callers_reg (fi, pi.framereg);
243 return fi->frame - pi.frameoffset;
246 /* Function: find_callers_reg
247 Find REGNUM on the stack. Otherwise, it's in an active register.
248 One thing we might want to do here is to check REGNUM against the
249 clobber mask, and somehow flag it as invalid if it isn't saved on
250 the stack somewhere. This would provide a graceful failure mode
251 when trying to get the value of caller-saves registers for an inner
255 mn10300_find_callers_reg (fi, regnum)
256 struct frame_info *fi;
259 printf("mn10300_find_callers_reg\n");
261 for (; fi; fi = fi->next)
262 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
263 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
264 else if (fi->fsr.regs[regnum] != 0)
265 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
266 REGISTER_RAW_SIZE(regnum));
268 return read_register (regnum);
271 /* Function: skip_prologue
272 Return the address of the first code past the prologue of the function. */
275 mn10300_skip_prologue (pc)
278 CORE_ADDR func_addr, func_end;
280 printf("mn10300_skip_prologue\n");
282 /* See what the symbol table says */
284 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
286 struct symtab_and_line sal;
288 sal = find_pc_line (func_addr, 0);
290 if (sal.line != 0 && sal.end < func_end)
293 /* Either there's no line info, or the line after the prologue is after
294 the end of the function. In this case, there probably isn't a
299 /* We can't find the start of this function, so there's nothing we can do. */
303 /* Function: pop_frame
304 This routine gets called when either the user uses the `return'
305 command, or the call dummy breakpoint gets hit. */
308 mn10300_pop_frame (frame)
309 struct frame_info *frame;
313 printf("mn10300_pop_frame start\n");
315 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
316 generic_pop_dummy_frame ();
319 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
321 for (regnum = 0; regnum < NUM_REGS; regnum++)
322 if (frame->fsr.regs[regnum] != 0)
323 write_register (regnum,
324 read_memory_unsigned_integer (frame->fsr.regs[regnum],
325 REGISTER_RAW_SIZE(regnum)));
327 write_register (SP_REGNUM, FRAME_FP (frame));
330 flush_cached_frames ();
332 printf("mn10300_pop_frame end\n");
335 /* Function: push_arguments
336 Setup arguments for a call to the target. Arguments go in
341 mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr)
345 unsigned char struct_return;
346 CORE_ADDR struct_addr;
350 int stack_offset = 0; /* copy args to this offset onto stack */
352 printf("mn10300_push_arguments start\n");
354 /* First, just for safety, make sure stack is aligned */
357 /* Now make space on the stack for the args. */
358 for (argnum = 0; argnum < nargs; argnum++)
359 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
363 /* Push all arguments onto the stack. */
364 for (argnum = 0; argnum < nargs; argnum++)
369 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
370 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
372 /* for now, pretend structs aren't special */
373 len = TYPE_LENGTH (VALUE_TYPE (*args));
374 val = (char *)VALUE_CONTENTS (*args);
378 len = TYPE_LENGTH (VALUE_TYPE (*args));
379 val = (char *)VALUE_CONTENTS (*args);
384 write_memory (sp + stack_offset, val, 4);
393 printf("mn10300_push_arguments end\n");
398 /* Function: push_return_address (pc)
399 Set up the return address for the inferior function call.
400 Needed for targets where we don't actually execute a JSR/BSR instruction */
403 mn10300_push_return_address (pc, sp)
407 printf("mn10300_push_return_address\n");
409 /* write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ()); */
413 /* Function: frame_saved_pc
414 Find the caller of this frame. We do this by seeing if RP_REGNUM
415 is saved in the stack anywhere, otherwise we get it from the
416 registers. If the inner frame is a dummy frame, return its PC
417 instead of RP, because that's where "caller" of the dummy-frame
421 mn10300_frame_saved_pc (fi)
422 struct frame_info *fi;
424 printf("mn10300_frame_saved_pc\n");
426 /* if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame)) */
427 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
429 return mn10300_find_callers_reg (fi, RP_REGNUM);
434 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
438 struct frame_info *frame;
440 enum lval_type *lval;
442 printf("get_saved_register\n");
444 generic_get_saved_register (raw_buffer, optimized, addrp,
445 frame, regnum, lval);
448 /* Function: fix_call_dummy
449 Pokes the callee function's address into the CALL_DUMMY assembly stub.
450 Assumes that the CALL_DUMMY looks like this:
456 mn10300_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
467 printf("mn10300_fix_call_dummy start\n");
469 offset24 = (long) fun - (long) entry_point_address ();
470 offset24 &= 0x3fffff;
471 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
473 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
474 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
476 printf("mn10300_fix_call_dummy end\n");
482 _initialize_mn10300_tdep ()
484 printf("_initialize_mn10300_tdep\n");
486 tm_print_insn = print_insn_mn10300;