1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, 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 /* Dummy frame. This saves the processor state just prior to setting up the
32 inferior function call. On most targets, the registers are saved on the
33 target stack, but that really slows down function calls. */
37 struct dummy_frame *next;
39 char regs[REGISTER_BYTES];
42 static struct dummy_frame *dummy_frame_stack = NULL;
44 static CORE_ADDR read_register_dummy PARAMS ((int regno));
46 /* Info gleaned from scanning a function's prologue. */
48 struct pifsr /* Info about one saved reg */
50 int framereg; /* Frame reg (SP or FP) */
51 int offset; /* Offset from framereg */
52 int reg; /* Saved register number */
63 static CORE_ADDR scan_prologue PARAMS ((CORE_ADDR pc, struct prologue_info *fs));
65 /* Scan the prologue of the function that contains PC, and record what we find
66 in PI. PI->fsr must be zeroed by the called. Returns the pc after the
67 prologue. Note that the addresses saved in pi->fsr are actually just frame
68 relative (negative offsets from the frame pointer). This is because we
69 don't know the actual value of the frame pointer yet. In some
70 circumstances, the frame pointer can't be determined till after we have
71 scanned the prologue. */
74 scan_prologue (pc, pi)
76 struct prologue_info *pi;
78 CORE_ADDR func_addr, prologue_end, current_pc;
82 /* First, figure out the bounds of the prologue so that we can limit the
83 search to something reasonable. */
85 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
87 struct symtab_and_line sal;
89 sal = find_pc_line (func_addr, 0);
91 if (func_addr == entry_point_address ())
92 pi->start_function = 1;
94 pi->start_function = 0;
100 prologue_end = sal.end;
106 { /* We're in the boondocks */
107 func_addr = pc - 100;
111 prologue_end = min (prologue_end, pc);
113 /* Now, search the prologue looking for instructions that setup fp, save
114 rp, adjust sp and such. We also record the frame offset of any saved
118 pi->framereg = SP_REGNUM;
122 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
126 insn = read_memory_unsigned_integer (current_pc, 2);
128 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
129 || (insn & 0xffe0) == 0x0060 /* jmp */
130 || (insn & 0x0780) == 0x0580) /* branch */
131 break; /* Ran into end of prologue */
132 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
133 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
134 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
135 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
136 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
139 pi->framereg = FP_REGNUM;
141 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
143 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
146 pifsr->framereg = insn & 0x1f;
147 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
149 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
154 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
159 pifsr->framereg = 0; /* Tie off last entry */
164 /* Setup the frame frame pointer, pc, and frame addresses for saved registers.
165 Most of the work is done in scan_prologue().
167 Note that when we are called for the last frame (currently active frame),
168 that fi->pc and fi->frame will already be setup. However, fi->frame will
169 be valid only if this routine uses FP. For previous frames, fi-frame will
170 always be correct (since that is derived from v850_frame_chain ()).
172 We can be called with the PC in the call dummy under two circumstances.
173 First, during normal backtracing, second, while figuring out the frame
174 pointer just prior to calling the target function (see run_stack_dummy).
178 v850_init_extra_frame_info (fi)
179 struct frame_info *fi;
181 struct prologue_info pi;
182 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
186 fi->pc = FRAME_SAVED_PC (fi->next);
188 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
190 /* The call dummy doesn't save any registers on the stack, so we can return
192 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
194 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
195 by assuming it's always FP. */
196 fi->frame = read_register_dummy (SP_REGNUM);
202 scan_prologue (fi->pc, &pi);
204 if (!fi->next && pi.framereg == SP_REGNUM)
205 fi->frame = read_register (pi.framereg) - pi.frameoffset;
207 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
209 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
211 if (pifsr->framereg == SP_REGNUM)
212 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
216 /* Figure out the frame prior to FI. Unfortunately, this involves scanning the
217 prologue of the caller, which will also be done shortly by
218 v850_init_extra_frame_info. For the dummy frame, we just return the stack
219 pointer that was in use at the time the function call was made. */
222 v850_frame_chain (fi)
223 struct frame_info *fi;
225 CORE_ADDR callers_pc;
226 struct prologue_info pi;
228 /* First, find out who called us */
230 callers_pc = FRAME_SAVED_PC (fi);
232 if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
233 return read_register_dummy (SP_REGNUM); /* XXX Won't work if multiple dummy frames on stack! */
237 scan_prologue (callers_pc, &pi);
239 if (pi.start_function)
240 return 0; /* Don't chain beyond the start function */
242 if (pi.framereg == FP_REGNUM)
243 return v850_find_callers_reg (fi, pi.framereg);
245 return fi->frame - pi.frameoffset;
248 /* Find REGNUM on the stack. Otherwise, it's in an active register. One thing
249 we might want to do here is to check REGNUM against the clobber mask, and
250 somehow flag it as invalid if it isn't saved on the stack somewhere. This
251 would provide a graceful failure mode when trying to get the value of
252 caller-saves registers for an inner frame. */
255 v850_find_callers_reg (fi, regnum)
256 struct frame_info *fi;
259 /* XXX - Won't work if multiple dummy frames are active */
260 /* When the caller requests RP from the dummy frame, we return PC because
261 that's where the previous routine appears to have done a call from. */
262 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
263 if (regnum == RP_REGNUM)
266 for (; fi; fi = fi->next)
267 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
268 return read_register_dummy (regnum);
269 else if (fi->fsr.regs[regnum] != 0)
270 return read_memory_integer (fi->fsr.regs[regnum], 4);
272 return read_register (regnum);
276 v850_skip_prologue (pc)
279 CORE_ADDR func_addr, func_end;
281 /* See what the symbol table says */
283 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
285 struct symtab_and_line sal;
287 sal = find_pc_line (func_addr, 0);
289 if (sal.line != 0 && sal.end < func_end)
292 /* Either there's no line info, or the line after the prologue is after
293 the end of the function. In this case, there probably isn't a
298 /* We can't find the start of this function, so there's nothing we can do. */
302 /* Save all the registers on the dummy frame stack. Most ports save the
303 registers on the target stack. This results in lots of unnecessary memory
304 references, which are slow when debugging via a serial line. Instead, we
305 save all the registers internally, and never write them to the stack. The
306 registers get restored when the called function returns to the entry point,
307 where a breakpoint is laying in wait. */
310 v850_push_dummy_frame ()
312 struct dummy_frame *dummy_frame;
314 dummy_frame = xmalloc (sizeof (struct dummy_frame));
316 read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
318 dummy_frame->next = dummy_frame_stack;
319 dummy_frame_stack = dummy_frame;
322 /* Read registers from the topmost dummy frame. */
325 read_register_dummy (regno)
328 return extract_address (&dummy_frame_stack->regs[REGISTER_BYTE (regno)],
329 REGISTER_RAW_SIZE(regno));
333 v850_pc_in_call_dummy (pc)
336 return dummy_frame_stack
337 && pc >= CALL_DUMMY_ADDRESS ()
338 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
341 /* This routine gets called when either the user uses the `return' command, or
342 the call dummy breakpoint gets hit. */
345 v850_pop_frame (frame)
346 struct frame_info *frame;
350 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
352 struct dummy_frame *dummy_frame;
354 dummy_frame = dummy_frame_stack;
356 error ("Can't pop dummy frame!");
358 dummy_frame_stack = dummy_frame->next;
360 write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
366 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
368 for (regnum = 0; regnum < NUM_REGS; regnum++)
369 if (frame->fsr.regs[regnum] != 0)
370 write_register (regnum,
371 read_memory_integer (frame->fsr.regs[regnum], 4));
373 write_register (SP_REGNUM, FRAME_FP (frame));
376 flush_cached_frames ();
381 /* Setup arguments and RP for a call to the target. First four args go in
382 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
383 reference. 64 bit quantities (doubles and long longs) may be split between
384 the regs and the stack. When calling a function that returns a struct, a
385 pointer to the struct is passed in as a secret first argument (always in R6).
387 By the time we get here, stack space has been allocated for the args, but
388 not for the struct return pointer. */
391 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
395 unsigned char struct_return;
396 CORE_ADDR struct_addr;
401 argreg = ARG0_REGNUM;
405 write_register (argreg++, struct_addr);
409 for (argnum = 0; argnum < nargs; argnum++)
415 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
416 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
418 store_address (valbuf, 4, VALUE_ADDRESS (*args));
424 len = TYPE_LENGTH (VALUE_TYPE (*args));
425 val = (char *)VALUE_CONTENTS (*args);
429 if (argreg <= ARGLAST_REGNUM)
433 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
434 write_register (argreg, regval);
436 len -= REGISTER_RAW_SIZE (argreg);
437 val += REGISTER_RAW_SIZE (argreg);
442 write_memory (sp + argnum * 4, val, 4);
450 write_register (RP_REGNUM, entry_point_address ());
456 _initialize_sparc_tdep ()
458 tm_print_insn = print_insn_v850;