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
;
69 /* First, figure out the bounds of the prologue so that we can limit the
70 search to something reasonable. */
72 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
))
74 struct symtab_and_line sal
;
76 sal
= find_pc_line (func_addr
, 0);
78 if (func_addr
== entry_point_address ())
79 pi
->start_function
= 1;
81 pi
->start_function
= 0;
87 prologue_end
= sal
.end
;
93 { /* We're in the boondocks */
98 prologue_end
= min (prologue_end
, pc
);
100 /* Now, search the prologue looking for instructions that setup fp, save
101 rp, adjust sp and such. We also record the frame offset of any saved
105 pi
->framereg
= SP_REGNUM
;
109 for (current_pc
= func_addr
; current_pc
< prologue_end
; current_pc
+= 2)
113 insn
= read_memory_unsigned_integer (current_pc
, 2);
115 if ((insn
& 0x07c0) == 0x0780 /* jarl or jr */
116 || (insn
& 0xffe0) == 0x0060 /* jmp */
117 || (insn
& 0x0780) == 0x0580) /* branch */
118 break; /* Ran into end of prologue */
119 if ((insn
& 0xffe0) == ((SP_REGNUM
<< 11) | 0x0240)) /* add <imm>,sp */
120 pi
->frameoffset
= ((insn
& 0x1f) ^ 0x10) - 0x10;
121 else if (insn
== ((SP_REGNUM
<< 11) | 0x0600 | SP_REGNUM
)) /* addi <imm>,sp,sp */
122 pi
->frameoffset
= read_memory_integer (current_pc
+ 2, 2);
123 else if (insn
== ((FP_REGNUM
<< 11) | 0x0000 | 12)) /* mov r12,fp */
126 pi
->framereg
= FP_REGNUM
;
128 else if ((insn
& 0x07ff) == (0x0760 | SP_REGNUM
) /* st.w <reg>,<offset>[sp] */
130 && (insn
& 0x07ff) == (0x0760 | FP_REGNUM
))) /* st.w <reg>,<offset>[fp] */
133 pifsr
->framereg
= insn
& 0x1f;
134 pifsr
->reg
= (insn
>> 11) & 0x1f; /* Extract <reg> */
136 pifsr
->offset
= read_memory_integer (current_pc
+ 2, 2) & ~1;
141 if ((insn
& 0x0780) >= 0x0600) /* Four byte instruction? */
146 pifsr
->framereg
= 0; /* Tie off last entry */
151 /* Function: init_extra_frame_info
152 Setup the frame's frame pointer, pc, and frame addresses for saved
153 registers. Most of the work is done in scan_prologue().
155 Note that when we are called for the last frame (currently active frame),
156 that fi->pc and fi->frame will already be setup. However, fi->frame will
157 be valid only if this routine uses FP. For previous frames, fi-frame will
158 always be correct (since that is derived from v850_frame_chain ()).
160 We can be called with the PC in the call dummy under two circumstances.
161 First, during normal backtracing, second, while figuring out the frame
162 pointer just prior to calling the target function (see run_stack_dummy). */
165 v850_init_extra_frame_info (fi
)
166 struct frame_info
*fi
;
168 struct prologue_info pi
;
169 struct pifsr pifsrs
[NUM_REGS
+ 1], *pifsr
;
173 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
175 memset (fi
->fsr
.regs
, '\000', sizeof fi
->fsr
.regs
);
177 /* The call dummy doesn't save any registers on the stack, so we can return
179 if (PC_IN_CALL_DUMMY (fi
->pc
, fi
->frame
, fi
->frame
))
184 v850_scan_prologue (fi
->pc
, &pi
);
186 if (!fi
->next
&& pi
.framereg
== SP_REGNUM
)
187 fi
->frame
= read_register (pi
.framereg
) - pi
.frameoffset
;
189 for (pifsr
= pifsrs
; pifsr
->framereg
; pifsr
++)
191 fi
->fsr
.regs
[pifsr
->reg
] = pifsr
->offset
+ fi
->frame
;
193 if (pifsr
->framereg
== SP_REGNUM
)
194 fi
->fsr
.regs
[pifsr
->reg
] += pi
.frameoffset
;
198 /* Function: frame_chain
199 Figure out the frame prior to FI. Unfortunately, this involves
200 scanning the prologue of the caller, which will also be done
201 shortly by v850_init_extra_frame_info. For the dummy frame, we
202 just return the stack pointer that was in use at the time the
203 function call was made. */
206 v850_frame_chain (fi
)
207 struct frame_info
*fi
;
209 struct prologue_info pi
;
210 CORE_ADDR callers_pc
, fp
;
212 /* First, find out who called us */
213 callers_pc
= FRAME_SAVED_PC (fi
);
214 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
215 fp
= v850_find_callers_reg (fi
, FP_REGNUM
);
216 if (PC_IN_CALL_DUMMY(callers_pc
, fp
, fp
))
217 return fp
; /* caller is call-dummy: return oldest value of FP */
219 /* Caller is NOT a call-dummy, so everything else should just work.
220 Even if THIS frame is a call-dummy! */
223 v850_scan_prologue (callers_pc
, &pi
);
225 if (pi
.start_function
)
226 return 0; /* Don't chain beyond the start function */
228 if (pi
.framereg
== FP_REGNUM
)
229 return v850_find_callers_reg (fi
, pi
.framereg
);
231 return fi
->frame
- pi
.frameoffset
;
234 /* Function: find_callers_reg
235 Find REGNUM on the stack. Otherwise, it's in an active register.
236 One thing we might want to do here is to check REGNUM against the
237 clobber mask, and somehow flag it as invalid if it isn't saved on
238 the stack somewhere. This would provide a graceful failure mode
239 when trying to get the value of caller-saves registers for an inner
243 v850_find_callers_reg (fi
, regnum
)
244 struct frame_info
*fi
;
247 for (; fi
; fi
= fi
->next
)
248 if (PC_IN_CALL_DUMMY (fi
->pc
, fi
->frame
, fi
->frame
))
249 return generic_read_register_dummy (fi
->pc
, fi
->frame
, regnum
);
250 else if (fi
->fsr
.regs
[regnum
] != 0)
251 return read_memory_unsigned_integer (fi
->fsr
.regs
[regnum
],
252 REGISTER_RAW_SIZE(regnum
));
254 return read_register (regnum
);
257 /* Function: skip_prologue
258 Return the address of the first code past the prologue of the function. */
261 v850_skip_prologue (pc
)
264 CORE_ADDR func_addr
, func_end
;
266 /* See what the symbol table says */
268 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
270 struct symtab_and_line sal
;
272 sal
= find_pc_line (func_addr
, 0);
274 if (sal
.line
!= 0 && sal
.end
< func_end
)
277 /* Either there's no line info, or the line after the prologue is after
278 the end of the function. In this case, there probably isn't a
283 /* We can't find the start of this function, so there's nothing we can do. */
287 /* Function: pop_frame
288 This routine gets called when either the user uses the `return'
289 command, or the call dummy breakpoint gets hit. */
292 v850_pop_frame (frame
)
293 struct frame_info
*frame
;
297 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
298 generic_pop_dummy_frame ();
301 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
303 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
304 if (frame
->fsr
.regs
[regnum
] != 0)
305 write_register (regnum
,
306 read_memory_unsigned_integer (frame
->fsr
.regs
[regnum
],
307 REGISTER_RAW_SIZE(regnum
)));
309 write_register (SP_REGNUM
, FRAME_FP (frame
));
312 flush_cached_frames ();
315 /* Function: push_arguments
316 Setup arguments and RP for a call to the target. First four args
317 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
318 are passed by reference. 64 bit quantities (doubles and long
319 longs) may be split between the regs and the stack. When calling a
320 function that returns a struct, a pointer to the struct is passed
321 in as a secret first argument (always in R6).
323 Stack space for the args has NOT been allocated: that job is up to us.
327 v850_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
331 unsigned char struct_return
;
332 CORE_ADDR struct_addr
;
339 /* First, just for safety, make sure stack is aligned */
342 /* Now make space on the stack for the args. */
343 for (argnum
= 0; argnum
< nargs
; argnum
++)
344 len
+= ((TYPE_LENGTH(VALUE_TYPE(args
[argnum
])) + 3) & ~3);
345 sp
-= len
; /* possibly over-allocating, but it works... */
346 /* (you might think we could allocate 16 bytes */
347 /* less, but the ABI seems to use it all! ) */
348 argreg
= ARG0_REGNUM
;
350 /* the struct_return pointer occupies the first parameter-passing reg */
352 write_register (argreg
++, struct_addr
);
355 /* The offset onto the stack at which we will start copying parameters
356 (after the registers are used up) begins at 16 rather than at zero.
357 I don't really know why, that's just the way it seems to work. */
359 /* Now load as many as possible of the first arguments into
360 registers, and push the rest onto the stack. There are 16 bytes
361 in four registers available. Loop thru args from first to last. */
362 for (argnum
= 0; argnum
< nargs
; argnum
++)
366 char valbuf
[REGISTER_RAW_SIZE(ARG0_REGNUM
)];
368 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
369 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
371 store_address (valbuf
, 4, VALUE_ADDRESS (*args
));
377 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
378 val
= (char *)VALUE_CONTENTS (*args
);
382 if (argreg
<= ARGLAST_REGNUM
)
386 regval
= extract_address (val
, REGISTER_RAW_SIZE (argreg
));
387 write_register (argreg
, regval
);
389 len
-= REGISTER_RAW_SIZE (argreg
);
390 val
+= REGISTER_RAW_SIZE (argreg
);
395 write_memory (sp
+ stack_offset
, val
, 4);
406 /* Function: push_return_address (pc)
407 Set up the return address for the inferior function call.
408 Needed for targets where we don't actually execute a JSR/BSR instruction */
410 #ifdef PUSH_RETURN_ADDRESS
412 v850_push_return_address (pc
, sp
)
416 #if CALL_DUMMY_LOCATION != AT_ENTRY_POINT
417 pc
= pc
- CALL_DUMMY_START_OFFSET
+ CALL_DUMMY_BREAKPOINT_OFFSET
;
419 pc
= CALL_DUMMY_ADDRESS ();
420 #endif /* CALL_DUMMY_LOCATION */
421 write_register (RP_REGNUM
, pc
);
424 #endif /* PUSH_RETURN_ADDRESS */
426 /* Function: frame_saved_pc
427 Find the caller of this frame. We do this by seeing if RP_REGNUM
428 is saved in the stack anywhere, otherwise we get it from the
429 registers. If the inner frame is a dummy frame, return its PC
430 instead of RP, because that's where "caller" of the dummy-frame
434 v850_frame_saved_pc (fi
)
435 struct frame_info
*fi
;
437 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
438 return generic_read_register_dummy(fi
->pc
, fi
->frame
, PC_REGNUM
);
440 return v850_find_callers_reg (fi
, RP_REGNUM
);
444 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
448 struct frame_info
*frame
;
450 enum lval_type
*lval
;
452 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
453 frame
, regnum
, lval
);
457 /* Function: fix_call_dummy
458 Pokes the callee function's address into the CALL_DUMMY assembly stub.
459 Assumes that the CALL_DUMMY looks like this:
465 v850_fix_call_dummy (dummy
, sp
, fun
, nargs
, args
, type
, gcc_p
)
475 CORE_ADDR call_dummy_start
;
476 #ifdef NEED_TEXT_START_END
477 extern CORE_ADDR text_end
;
480 #if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
481 call_dummy_start
= entry_point_address ();
482 #elif (CALL_DUMMY_LOCATION == AFTER_TEXT_END)
483 call_dummy_start
= text_end
;
484 #elif (CALL_DUMMY_LOCATION == BEFORE_TEXT_END)
485 call_dummy_start
= (text_end
- CALL_DUMMY_LENGTH
) & ~3;
486 #elif (CALL_DUMMY_LOCATION == ON_STACK)
487 call_dummy_start
= sp
;
490 offset24
= (long) fun
- (long) call_dummy_start
;
491 offset24
&= 0x3fffff;
492 offset24
|= 0xff800000; /* jarl <offset24>, r31 */
494 store_unsigned_integer ((unsigned int *)&dummy
[2], 2, offset24
& 0xffff);
495 store_unsigned_integer ((unsigned int *)&dummy
[0], 2, offset24
>> 16);
500 _initialize_v850_tdep ()
502 tm_print_insn
= print_insn_v850
;