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"
32 struct dummy_frame *next;
40 static struct dummy_frame *dummy_frame_stack = NULL;
42 /* This function actually figures out the frame address for a given pc and
43 sp. This is tricky on the v850 because we only use an explicit frame
44 pointer when using alloca(). The only reliable way to get this info is to
49 v850_init_extra_frame_info (fi)
50 struct frame_info *fi;
52 struct symtab_and_line sal;
53 CORE_ADDR func_addr, prologue_end, current_pc;
59 fi->pc = FRAME_SAVED_PC (fi->next);
61 /* First, figure out the bounds of the prologue so that we can limit the
62 search to something reasonable. */
64 if (find_pc_partial_function (fi->pc, NULL, &func_addr, NULL))
66 sal = find_pc_line (func_addr, 0);
69 prologue_end = fi->pc;
71 prologue_end = sal.end;
74 prologue_end = func_addr + 100; /* We're in the boondocks */
76 prologue_end = min (prologue_end, fi->pc);
78 /* Now, search the prologue looking for instructions that setup fp, save
79 rp, adjust sp and such. */
83 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
85 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
89 insn = read_memory_integer (current_pc, 2);
91 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
92 frameoffset = (insn & 0x1f) | ~0x1f;
93 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
97 frameoffset = read_memory_integer (current_pc, 2);
99 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
100 framereg = FP_REGNUM; /* Setting up fp */
101 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
103 reg = (insn >> 11) & 0x1f; /* Extract <reg> */
106 insn = read_memory_integer (current_pc, 2) & ~1;
108 fi->fsr.regs[reg] = insn + frameoffset;
110 else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
112 reg = (insn >> 11) & 0x1f; /* Extract <reg> */
115 insn = read_memory_integer (current_pc, 2) & ~1;
117 fi->fsr.regs[reg] = insn;
121 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
122 fi->frame = dummy_frame_stack->sp;
123 else if (!fi->next && framereg == SP_REGNUM)
124 fi->frame = read_register (framereg) - frameoffset;
126 for (reg = 0; reg < NUM_REGS; reg++)
127 if (fi->fsr.regs[reg] != 0)
128 fi->fsr.regs[reg] += fi->frame;
131 /* Find the caller of this frame. We do this by seeing if RP_REGNUM is saved
132 in the stack anywhere, otherwise we get it from the registers. */
135 v850_find_callers_reg (fi, regnum)
136 struct frame_info *fi;
139 /* XXX - Won't work if multiple dummy frames are active */
140 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
144 return dummy_frame_stack->sp;
147 return dummy_frame_stack->fp;
150 return dummy_frame_stack->pc;
153 return dummy_frame_stack->pc;
157 for (; fi; fi = fi->next)
158 if (fi->fsr.regs[regnum] != 0)
159 return read_memory_integer (fi->fsr.regs[regnum], 4);
161 return read_register (regnum);
165 v850_frame_chain (fi)
166 struct frame_info *fi;
168 CORE_ADDR callers_pc, callers_sp;
169 CORE_ADDR func_addr, prologue_end, current_pc;
172 /* First, find out who called us */
174 callers_pc = FRAME_SAVED_PC (fi);
176 if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
177 return dummy_frame_stack->sp; /* XXX Won't work if multiple dummy frames on stack! */
179 /* Next, figure out where his prologue is. */
181 if (find_pc_partial_function (callers_pc, NULL, &func_addr, NULL))
183 struct symtab_and_line sal;
185 /* Stop when the caller is the entry point function */
186 if (func_addr == entry_point_address ())
189 sal = find_pc_line (func_addr, 0);
192 prologue_end = callers_pc;
194 prologue_end = sal.end;
197 prologue_end = func_addr + 100; /* We're in the boondocks */
199 prologue_end = min (prologue_end, callers_pc);
201 /* Now, figure out the frame location of the caller by examining his prologue.
202 We're looking for either a load of the frame pointer register, or a stack
207 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
211 insn = read_memory_integer (current_pc, 2);
213 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
214 frameoffset = (insn & 0x1f) | ~0x1f;
215 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
219 frameoffset = read_memory_integer (current_pc, 2);
221 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
222 return v850_find_callers_reg (fi, FP_REGNUM); /* It's using a frame pointer reg */
223 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
225 else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
229 return fi->frame - frameoffset;
233 v850_skip_prologue (pc)
236 CORE_ADDR func_addr, func_end;
238 /* See what the symbol table says */
240 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
242 struct symtab_and_line sal;
244 sal = find_pc_line (func_addr, 0);
246 if (sal.line != 0 && sal.end < func_end)
249 /* Either there's no line info, or the line after the prologue is after
250 the end of the function. In this case, there probably isn't a
255 /* We can't find the start of this function, so there's nothing we can do. */
259 /* All we do here is record SP and FP on the call dummy stack */
262 v850_push_dummy_frame ()
264 struct dummy_frame *dummy_frame;
266 dummy_frame = xmalloc (sizeof (struct dummy_frame));
268 dummy_frame->fp = read_register (FP_REGNUM);
269 dummy_frame->sp = read_register (SP_REGNUM);
270 dummy_frame->rp = read_register (RP_REGNUM);
271 dummy_frame->pc = read_register (PC_REGNUM);
272 dummy_frame->next = dummy_frame_stack;
273 dummy_frame_stack = dummy_frame;
277 v850_pc_in_call_dummy (pc)
280 return dummy_frame_stack
281 && pc >= CALL_DUMMY_ADDRESS ()
282 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
286 v850_pop_frame (frame)
287 struct frame_info *frame;
291 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
293 struct dummy_frame *dummy_frame;
295 dummy_frame = dummy_frame_stack;
297 error ("Can't pop dummy frame!");
299 dummy_frame_stack = dummy_frame->next;
301 write_register (FP_REGNUM, dummy_frame->fp);
302 write_register (SP_REGNUM, dummy_frame->sp);
303 write_register (RP_REGNUM, dummy_frame->rp);
304 write_register (PC_REGNUM, dummy_frame->pc);
308 flush_cached_frames ();
313 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
315 for (regnum = 0; regnum < NUM_REGS; regnum++)
316 if (frame->fsr.regs[regnum] != 0)
317 write_register (regnum, read_memory_integer (frame->fsr.regs[regnum], 4));
319 write_register (SP_REGNUM, FRAME_FP (frame));
320 flush_cached_frames ();
325 /* Put arguments in the right places, and setup return address register (RP) to
326 point at a convenient place to put a breakpoint. First four args go in
327 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
328 reference. 64 bit quantities (doubles and long longs) may be split between
329 the regs and the stack. When calling a function that returns a struct, a
330 pointer to the struct is passed in as a secret first argument (always in R6).
332 By the time we get here, stack space has been allocated for the args, but
333 not for the struct return pointer. */
336 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
340 unsigned char struct_return;
341 CORE_ADDR struct_addr;
350 write_register (argreg++, struct_addr);
354 for (argnum = 0; argnum < nargs; argnum++)
360 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
361 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
363 store_address (valbuf, 4, VALUE_ADDRESS (*args));
369 len = TYPE_LENGTH (VALUE_TYPE (*args));
370 val = (char *)VALUE_CONTENTS (*args);
378 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
379 write_register (argreg, regval);
381 len -= REGISTER_RAW_SIZE (argreg);
382 val += REGISTER_RAW_SIZE (argreg);
387 write_memory (sp + argnum * 4, val, 4);
395 write_register (RP_REGNUM, entry_point_address ());
401 _initialize_sparc_tdep ()
403 tm_print_insn = print_insn_v850;