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 /* 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 v850_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 v850_scan_prologue (pc, pi)
63 struct prologue_info *pi;
65 CORE_ADDR func_addr, prologue_end, current_pc;
70 /* First, figure out the bounds of the prologue so that we can limit the
71 search to something reasonable. */
73 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
75 struct symtab_and_line sal;
77 sal = find_pc_line (func_addr, 0);
79 if (func_addr == entry_point_address ())
80 pi->start_function = 1;
82 pi->start_function = 0;
88 prologue_end = sal.end;
94 { /* We're in the boondocks */
99 prologue_end = min (prologue_end, pc);
101 /* Now, search the prologue looking for instructions that setup fp, save
102 rp, adjust sp and such. We also record the frame offset of any saved
106 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 == 0xf003) /* mov sp,ep */
132 else if (insn == 0xf001) /* mov r1,ep */
134 else if (((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
136 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
139 pifsr->framereg = insn & 0x1f;
140 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
141 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
145 else if (ep_used /* sst.w <reg>,<offset>[ep] */
146 && ((insn & 0x0781) == 0x0501)
150 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
151 pifsr->offset = (insn & 0x007e) << 2;
155 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
160 pifsr->framereg = 0; /* Tie off last entry */
165 /* Function: init_extra_frame_info
166 Setup the frame's frame pointer, pc, and frame addresses for saved
167 registers. Most of the work is done in scan_prologue().
169 Note that when we are called for the last frame (currently active frame),
170 that fi->pc and fi->frame will already be setup. However, fi->frame will
171 be valid only if this routine uses FP. For previous frames, fi-frame will
172 always be correct (since that is derived from v850_frame_chain ()).
174 We can be called with the PC in the call dummy under two circumstances.
175 First, during normal backtracing, second, while figuring out the frame
176 pointer just prior to calling the target function (see run_stack_dummy). */
179 v850_init_extra_frame_info (fi)
180 struct frame_info *fi;
182 struct prologue_info pi;
183 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
187 fi->pc = FRAME_SAVED_PC (fi->next);
189 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
191 /* The call dummy doesn't save any registers on the stack, so we can return
193 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
198 v850_scan_prologue (fi->pc, &pi);
200 if (!fi->next && pi.framereg == SP_REGNUM)
201 fi->frame = read_register (pi.framereg) - pi.frameoffset;
203 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
205 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
207 if (pifsr->framereg == SP_REGNUM)
208 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
212 /* Function: frame_chain
213 Figure out the frame prior to FI. Unfortunately, this involves
214 scanning the prologue of the caller, which will also be done
215 shortly by v850_init_extra_frame_info. For the dummy frame, we
216 just return the stack pointer that was in use at the time the
217 function call was made. */
220 v850_frame_chain (fi)
221 struct frame_info *fi;
223 struct prologue_info pi;
224 CORE_ADDR callers_pc, fp;
226 /* First, find out who called us */
227 callers_pc = FRAME_SAVED_PC (fi);
228 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
229 fp = v850_find_callers_reg (fi, FP_REGNUM);
230 if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
231 return fp; /* caller is call-dummy: return oldest value of FP */
233 /* Caller is NOT a call-dummy, so everything else should just work.
234 Even if THIS frame is a call-dummy! */
237 v850_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 /* Function: find_callers_reg
249 Find REGNUM on the stack. Otherwise, it's in an active register.
250 One thing we might want to do here is to check REGNUM against the
251 clobber mask, and somehow flag it as invalid if it isn't saved on
252 the stack somewhere. This would provide a graceful failure mode
253 when trying to get the value of caller-saves registers for an inner
257 v850_find_callers_reg (fi, regnum)
258 struct frame_info *fi;
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 v850_skip_prologue (pc)
278 CORE_ADDR func_addr, func_end;
280 /* See what the symbol table says */
282 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
284 struct symtab_and_line sal;
286 sal = find_pc_line (func_addr, 0);
288 if (sal.line != 0 && sal.end < func_end)
291 /* Either there's no line info, or the line after the prologue is after
292 the end of the function. In this case, there probably isn't a
297 /* We can't find the start of this function, so there's nothing we can do. */
301 /* Function: pop_frame
302 This routine gets called when either the user uses the `return'
303 command, or the call dummy breakpoint gets hit. */
306 v850_pop_frame (frame)
307 struct frame_info *frame;
311 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
312 generic_pop_dummy_frame ();
315 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
317 for (regnum = 0; regnum < NUM_REGS; regnum++)
318 if (frame->fsr.regs[regnum] != 0)
319 write_register (regnum,
320 read_memory_unsigned_integer (frame->fsr.regs[regnum],
321 REGISTER_RAW_SIZE(regnum)));
323 write_register (SP_REGNUM, FRAME_FP (frame));
326 flush_cached_frames ();
329 /* Function: push_arguments
330 Setup arguments and RP for a call to the target. First four args
331 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
332 are passed by reference. 64 bit quantities (doubles and long
333 longs) may be split between the regs and the stack. When calling a
334 function that returns a struct, a pointer to the struct is passed
335 in as a secret first argument (always in R6).
337 Stack space for the args has NOT been allocated: that job is up to us.
341 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
345 unsigned char struct_return;
346 CORE_ADDR struct_addr;
353 /* First, just for safety, make sure stack is aligned */
356 /* Now make space on the stack for the args. */
357 for (argnum = 0; argnum < nargs; argnum++)
358 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
359 sp -= len; /* possibly over-allocating, but it works... */
360 /* (you might think we could allocate 16 bytes */
361 /* less, but the ABI seems to use it all! ) */
362 argreg = ARG0_REGNUM;
364 /* the struct_return pointer occupies the first parameter-passing reg */
366 write_register (argreg++, struct_addr);
369 /* The offset onto the stack at which we will start copying parameters
370 (after the registers are used up) begins at 16 rather than at zero.
371 I don't really know why, that's just the way it seems to work. */
373 /* Now load as many as possible of the first arguments into
374 registers, and push the rest onto the stack. There are 16 bytes
375 in four registers available. Loop thru args from first to last. */
376 for (argnum = 0; argnum < nargs; argnum++)
380 char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
382 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
383 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
385 store_address (valbuf, 4, VALUE_ADDRESS (*args));
391 len = TYPE_LENGTH (VALUE_TYPE (*args));
392 val = (char *)VALUE_CONTENTS (*args);
396 if (argreg <= ARGLAST_REGNUM)
400 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
401 write_register (argreg, regval);
403 len -= REGISTER_RAW_SIZE (argreg);
404 val += REGISTER_RAW_SIZE (argreg);
409 write_memory (sp + stack_offset, val, 4);
420 /* Function: push_return_address (pc)
421 Set up the return address for the inferior function call.
422 Needed for targets where we don't actually execute a JSR/BSR instruction */
425 v850_push_return_address (pc, sp)
429 write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
433 /* Function: frame_saved_pc
434 Find the caller of this frame. We do this by seeing if RP_REGNUM
435 is saved in the stack anywhere, otherwise we get it from the
436 registers. If the inner frame is a dummy frame, return its PC
437 instead of RP, because that's where "caller" of the dummy-frame
441 v850_frame_saved_pc (fi)
442 struct frame_info *fi;
444 if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
445 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
447 return v850_find_callers_reg (fi, RP_REGNUM);
451 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
455 struct frame_info *frame;
457 enum lval_type *lval;
459 generic_get_saved_register (raw_buffer, optimized, addrp,
460 frame, regnum, lval);
464 /* Function: fix_call_dummy
465 Pokes the callee function's address into the CALL_DUMMY assembly stub.
466 Assumes that the CALL_DUMMY looks like this:
472 v850_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
483 offset24 = (long) fun - (long) entry_point_address ();
484 offset24 &= 0x3fffff;
485 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
487 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
488 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
493 _initialize_v850_tdep ()
495 tm_print_insn = print_insn_v850;