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_unsigned_integer (current_pc, 2);
91 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
92 frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
93 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
94 frameoffset = read_memory_integer (current_pc + 2, 2);
95 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
96 framereg = FP_REGNUM; /* Setting up fp */
97 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
99 reg = (insn >> 11) & 0x1f; /* Extract <reg> */
101 insn = read_memory_integer (current_pc + 2, 2) & ~1;
103 fi->fsr.regs[reg] = insn + frameoffset;
105 else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
107 reg = (insn >> 11) & 0x1f; /* Extract <reg> */
109 insn = read_memory_integer (current_pc + 2, 2) & ~1;
111 fi->fsr.regs[reg] = insn;
114 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
118 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
119 fi->frame = dummy_frame_stack->sp;
120 else if (!fi->next && framereg == SP_REGNUM)
121 fi->frame = read_register (framereg) - frameoffset;
123 for (reg = 0; reg < NUM_REGS; reg++)
124 if (fi->fsr.regs[reg] != 0)
125 fi->fsr.regs[reg] += fi->frame;
128 /* Find the caller of this frame. We do this by seeing if RP_REGNUM is saved
129 in the stack anywhere, otherwise we get it from the registers. */
132 v850_find_callers_reg (fi, regnum)
133 struct frame_info *fi;
136 /* XXX - Won't work if multiple dummy frames are active */
137 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
141 return dummy_frame_stack->sp;
144 return dummy_frame_stack->fp;
147 return dummy_frame_stack->pc;
150 return dummy_frame_stack->pc;
154 for (; fi; fi = fi->next)
155 if (fi->fsr.regs[regnum] != 0)
156 return read_memory_integer (fi->fsr.regs[regnum], 4);
158 return read_register (regnum);
162 v850_frame_chain (fi)
163 struct frame_info *fi;
165 CORE_ADDR callers_pc, callers_sp;
166 CORE_ADDR func_addr, prologue_end, current_pc;
169 /* First, find out who called us */
171 callers_pc = FRAME_SAVED_PC (fi);
173 if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
174 return dummy_frame_stack->sp; /* XXX Won't work if multiple dummy frames on stack! */
176 /* Next, figure out where his prologue is. */
178 if (find_pc_partial_function (callers_pc, NULL, &func_addr, NULL))
180 struct symtab_and_line sal;
182 /* Stop when the caller is the entry point function */
183 if (func_addr == entry_point_address ())
186 sal = find_pc_line (func_addr, 0);
189 prologue_end = callers_pc;
191 prologue_end = sal.end;
194 prologue_end = func_addr + 100; /* We're in the boondocks */
196 prologue_end = min (prologue_end, callers_pc);
198 /* Now, figure out the frame location of the caller by examining his prologue.
199 We're looking for either a load of the frame pointer register, or a stack
204 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
208 insn = read_memory_unsigned_integer (current_pc, 2);
210 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
211 frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
212 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
213 frameoffset = read_memory_integer (current_pc + 2, 2);
214 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
215 return v850_find_callers_reg (fi, FP_REGNUM); /* It's using a frame pointer reg */
217 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
221 return fi->frame - frameoffset;
225 v850_skip_prologue (pc)
228 CORE_ADDR func_addr, func_end;
230 /* See what the symbol table says */
232 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
234 struct symtab_and_line sal;
236 sal = find_pc_line (func_addr, 0);
238 if (sal.line != 0 && sal.end < func_end)
241 /* Either there's no line info, or the line after the prologue is after
242 the end of the function. In this case, there probably isn't a
247 /* We can't find the start of this function, so there's nothing we can do. */
251 /* All we do here is record SP and FP on the call dummy stack */
254 v850_push_dummy_frame ()
256 struct dummy_frame *dummy_frame;
258 dummy_frame = xmalloc (sizeof (struct dummy_frame));
260 dummy_frame->fp = read_register (FP_REGNUM);
261 dummy_frame->sp = read_register (SP_REGNUM);
262 dummy_frame->rp = read_register (RP_REGNUM);
263 dummy_frame->pc = read_register (PC_REGNUM);
264 dummy_frame->next = dummy_frame_stack;
265 dummy_frame_stack = dummy_frame;
269 v850_pc_in_call_dummy (pc)
272 return dummy_frame_stack
273 && pc >= CALL_DUMMY_ADDRESS ()
274 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
278 v850_pop_frame (frame)
279 struct frame_info *frame;
283 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
285 struct dummy_frame *dummy_frame;
287 dummy_frame = dummy_frame_stack;
289 error ("Can't pop dummy frame!");
291 dummy_frame_stack = dummy_frame->next;
293 write_register (FP_REGNUM, dummy_frame->fp);
294 write_register (SP_REGNUM, dummy_frame->sp);
295 write_register (RP_REGNUM, dummy_frame->rp);
296 write_register (PC_REGNUM, dummy_frame->pc);
300 flush_cached_frames ();
305 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
307 for (regnum = 0; regnum < NUM_REGS; regnum++)
308 if (frame->fsr.regs[regnum] != 0)
309 write_register (regnum, read_memory_integer (frame->fsr.regs[regnum], 4));
311 write_register (SP_REGNUM, FRAME_FP (frame));
312 flush_cached_frames ();
317 /* Put arguments in the right places, and setup return address register (RP) to
318 point at a convenient place to put a breakpoint. First four args go in
319 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
320 reference. 64 bit quantities (doubles and long longs) may be split between
321 the regs and the stack. When calling a function that returns a struct, a
322 pointer to the struct is passed in as a secret first argument (always in R6).
324 By the time we get here, stack space has been allocated for the args, but
325 not for the struct return pointer. */
328 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
332 unsigned char struct_return;
333 CORE_ADDR struct_addr;
338 argreg = ARG0_REGNUM;
342 write_register (argreg++, struct_addr);
346 for (argnum = 0; argnum < nargs; argnum++)
352 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
353 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
355 store_address (valbuf, 4, VALUE_ADDRESS (*args));
361 len = TYPE_LENGTH (VALUE_TYPE (*args));
362 val = (char *)VALUE_CONTENTS (*args);
366 if (argreg <= ARGLAST_REGNUM)
370 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
371 write_register (argreg, regval);
373 len -= REGISTER_RAW_SIZE (argreg);
374 val += REGISTER_RAW_SIZE (argreg);
379 write_memory (sp + argnum * 4, val, 4);
387 write_register (RP_REGNUM, entry_point_address ());
393 _initialize_sparc_tdep ()
395 tm_print_insn = print_insn_v850;