Tue Nov 26 18:29:23 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / v850-tdep.c
CommitLineData
e2810631 1/* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
ac954805 2 Copyright 1996, Free Software Foundation, Inc.
e2810631
SG
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
e2810631
SG
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "obstack.h"
24#include "target.h"
25#include "value.h"
26#include "bfd.h"
27#include "gdb_string.h"
e2810631 28#include "gdbcore.h"
23da411a
SG
29#include "symfile.h"
30
23da411a
SG
31/* Info gleaned from scanning a function's prologue. */
32
a638512f
SG
33struct pifsr /* Info about one saved reg */
34{
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int reg; /* Saved register number */
38};
39
23da411a 40struct prologue_info
e5a2ac8b 41{
e5a2ac8b 42 int framereg;
23da411a
SG
43 int frameoffset;
44 int start_function;
a638512f 45 struct pifsr *pifsrs;
23da411a 46};
e5a2ac8b 47
dc1b349d
MS
48static CORE_ADDR v850_scan_prologue PARAMS ((CORE_ADDR pc,
49 struct prologue_info *fs));
23da411a 50\f
dc1b349d
MS
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. */
23da411a
SG
59
60static CORE_ADDR
dc1b349d 61v850_scan_prologue (pc, pi)
23da411a
SG
62 CORE_ADDR pc;
63 struct prologue_info *pi;
64{
65 CORE_ADDR func_addr, prologue_end, current_pc;
a638512f 66 struct pifsr *pifsr;
23da411a 67 int fp_used;
e5a2ac8b
SG
68
69 /* First, figure out the bounds of the prologue so that we can limit the
70 search to something reasonable. */
71
23da411a 72 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
e5a2ac8b 73 {
23da411a
SG
74 struct symtab_and_line sal;
75
e5a2ac8b
SG
76 sal = find_pc_line (func_addr, 0);
77
23da411a
SG
78 if (func_addr == entry_point_address ())
79 pi->start_function = 1;
80 else
81 pi->start_function = 0;
82
a638512f 83#if 0
ac954805 84 if (sal.line == 0)
23da411a 85 prologue_end = pc;
ac954805
SG
86 else
87 prologue_end = sal.end;
a638512f
SG
88#else
89 prologue_end = pc;
90#endif
e5a2ac8b
SG
91 }
92 else
23da411a
SG
93 { /* We're in the boondocks */
94 func_addr = pc - 100;
95 prologue_end = pc;
96 }
e5a2ac8b 97
23da411a 98 prologue_end = min (prologue_end, pc);
e5a2ac8b
SG
99
100 /* Now, search the prologue looking for instructions that setup fp, save
23da411a
SG
101 rp, adjust sp and such. We also record the frame offset of any saved
102 registers. */
e5a2ac8b 103
23da411a
SG
104 pi->frameoffset = 0;
105 pi->framereg = SP_REGNUM;
106 fp_used = 0;
a638512f 107 pifsr = pi->pifsrs;
e5a2ac8b
SG
108
109 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
110 {
111 int insn;
112
6420594b 113 insn = read_memory_unsigned_integer (current_pc, 2);
e5a2ac8b 114
a638512f
SG
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 */
e5a2ac8b 119 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
23da411a 120 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
e5a2ac8b 121 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
23da411a
SG
122 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
123 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
e5a2ac8b 124 {
23da411a
SG
125 fp_used = 1;
126 pi->framereg = FP_REGNUM;
127 }
128 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
129 || (fp_used
130 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
a638512f 131 if (pifsr)
23da411a 132 {
a638512f
SG
133 pifsr->framereg = insn & 0x1f;
134 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
e5a2ac8b 135
a638512f 136 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
e5a2ac8b 137
a638512f 138 pifsr++;
23da411a 139 }
6420594b
SG
140
141 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
142 current_pc += 2;
e5a2ac8b
SG
143 }
144
a638512f
SG
145 if (pifsr)
146 pifsr->framereg = 0; /* Tie off last entry */
147
23da411a 148 return current_pc;
e5a2ac8b
SG
149}
150
dc1b349d
MS
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().
e5a2ac8b 154
23da411a
SG
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 ()).
159
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
dc1b349d 162 pointer just prior to calling the target function (see run_stack_dummy). */
23da411a
SG
163
164void
165v850_init_extra_frame_info (fi)
e5a2ac8b 166 struct frame_info *fi;
e5a2ac8b 167{
23da411a 168 struct prologue_info pi;
a638512f 169 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
23da411a
SG
170 int reg;
171
172 if (fi->next)
173 fi->pc = FRAME_SAVED_PC (fi->next);
174
175 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
176
177 /* The call dummy doesn't save any registers on the stack, so we can return
178 now. */
dc1b349d 179 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
23da411a 180 return;
ac954805 181
a638512f 182 pi.pifsrs = pifsrs;
e5a2ac8b 183
dc1b349d 184 v850_scan_prologue (fi->pc, &pi);
23da411a 185
a638512f
SG
186 if (!fi->next && pi.framereg == SP_REGNUM)
187 fi->frame = read_register (pi.framereg) - pi.frameoffset;
23da411a 188
a638512f
SG
189 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
190 {
191 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
192
193 if (pifsr->framereg == SP_REGNUM)
194 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
195 }
e5a2ac8b
SG
196}
197
dc1b349d
MS
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. */
23da411a 204
e5a2ac8b
SG
205CORE_ADDR
206v850_frame_chain (fi)
207 struct frame_info *fi;
208{
23da411a 209 struct prologue_info pi;
dc1b349d 210 CORE_ADDR callers_pc, fp;
e5a2ac8b
SG
211
212 /* First, find out who called us */
ac954805 213 callers_pc = FRAME_SAVED_PC (fi);
dc1b349d
MS
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 */
ac954805 218
dc1b349d
MS
219 /* Caller is NOT a call-dummy, so everything else should just work.
220 Even if THIS frame is a call-dummy! */
a638512f 221 pi.pifsrs = NULL;
e5a2ac8b 222
dc1b349d 223 v850_scan_prologue (callers_pc, &pi);
e5a2ac8b 224
23da411a
SG
225 if (pi.start_function)
226 return 0; /* Don't chain beyond the start function */
ac954805 227
23da411a
SG
228 if (pi.framereg == FP_REGNUM)
229 return v850_find_callers_reg (fi, pi.framereg);
e5a2ac8b 230
23da411a
SG
231 return fi->frame - pi.frameoffset;
232}
e5a2ac8b 233
dc1b349d
MS
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
240 frame. */
e5a2ac8b 241
23da411a
SG
242CORE_ADDR
243v850_find_callers_reg (fi, regnum)
244 struct frame_info *fi;
245 int regnum;
246{
23da411a 247 for (; fi; fi = fi->next)
dc1b349d
MS
248 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
249 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
23da411a 250 else if (fi->fsr.regs[regnum] != 0)
dc1b349d
MS
251 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
252 REGISTER_RAW_SIZE(regnum));
e5a2ac8b 253
23da411a 254 return read_register (regnum);
e5a2ac8b
SG
255}
256
dc1b349d
MS
257/* Function: skip_prologue
258 Return the address of the first code past the prologue of the function. */
259
e5a2ac8b
SG
260CORE_ADDR
261v850_skip_prologue (pc)
262 CORE_ADDR pc;
263{
264 CORE_ADDR func_addr, func_end;
265
266 /* See what the symbol table says */
267
268 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
269 {
270 struct symtab_and_line sal;
271
272 sal = find_pc_line (func_addr, 0);
273
ac954805 274 if (sal.line != 0 && sal.end < func_end)
e5a2ac8b
SG
275 return sal.end;
276 else
ac954805
SG
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
279 prologue. */
e5a2ac8b
SG
280 return pc;
281 }
282
283/* We can't find the start of this function, so there's nothing we can do. */
284 return pc;
285}
286
dc1b349d
MS
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. */
ac954805
SG
290
291void
e5a2ac8b
SG
292v850_pop_frame (frame)
293 struct frame_info *frame;
294{
295 int regnum;
296
dc1b349d
MS
297 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
298 generic_pop_dummy_frame ();
23da411a
SG
299 else
300 {
301 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
ac954805 302
23da411a
SG
303 for (regnum = 0; regnum < NUM_REGS; regnum++)
304 if (frame->fsr.regs[regnum] != 0)
305 write_register (regnum,
dc1b349d
MS
306 read_memory_unsigned_integer (frame->fsr.regs[regnum],
307 REGISTER_RAW_SIZE(regnum)));
e5a2ac8b 308
23da411a
SG
309 write_register (SP_REGNUM, FRAME_FP (frame));
310 }
e5a2ac8b 311
e5a2ac8b 312 flush_cached_frames ();
e5a2ac8b 313}
ac954805 314
dc1b349d
MS
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).
ac954805 322
dc1b349d
MS
323 Stack space for the args has NOT been allocated: that job is up to us.
324 */
ac954805
SG
325
326CORE_ADDR
327v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
328 int nargs;
329 value_ptr *args;
330 CORE_ADDR sp;
331 unsigned char struct_return;
332 CORE_ADDR struct_addr;
333{
334 int argreg;
335 int argnum;
dc1b349d
MS
336 int len = 0;
337 int stack_offset;
ac954805 338
dc1b349d
MS
339 /* First, just for safety, make sure stack is aligned */
340 sp &= ~3;
341
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! ) */
687f4e23 348 argreg = ARG0_REGNUM;
ac954805 349
dc1b349d 350 /* the struct_return pointer occupies the first parameter-passing reg */
ac954805 351 if (struct_return)
ac954805 352 write_register (argreg++, struct_addr);
ac954805 353
dc1b349d
MS
354 stack_offset = 16;
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. */
358
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. */
ac954805
SG
362 for (argnum = 0; argnum < nargs; argnum++)
363 {
364 int len;
365 char *val;
dc1b349d 366 char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
ac954805
SG
367
368 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
369 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
370 {
371 store_address (valbuf, 4, VALUE_ADDRESS (*args));
372 len = 4;
373 val = valbuf;
374 }
375 else
376 {
377 len = TYPE_LENGTH (VALUE_TYPE (*args));
378 val = (char *)VALUE_CONTENTS (*args);
379 }
380
381 while (len > 0)
687f4e23 382 if (argreg <= ARGLAST_REGNUM)
ac954805
SG
383 {
384 CORE_ADDR regval;
385
386 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
387 write_register (argreg, regval);
388
389 len -= REGISTER_RAW_SIZE (argreg);
390 val += REGISTER_RAW_SIZE (argreg);
391 argreg++;
392 }
393 else
394 {
dc1b349d 395 write_memory (sp + stack_offset, val, 4);
ac954805
SG
396
397 len -= 4;
398 val += 4;
dc1b349d 399 stack_offset += 4;
ac954805
SG
400 }
401 args++;
402 }
dc1b349d
MS
403 return sp;
404}
ac954805 405
dc1b349d
MS
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 */
409
410#ifdef PUSH_RETURN_ADDRESS
411CORE_ADDR
412v850_push_return_address (pc, sp)
413 CORE_ADDR pc;
414 CORE_ADDR sp;
415{
416#if CALL_DUMMY_LOCATION != AT_ENTRY_POINT
417 pc = pc - CALL_DUMMY_START_OFFSET + CALL_DUMMY_BREAKPOINT_OFFSET;
418#else
419 pc = CALL_DUMMY_ADDRESS ();
420#endif /* CALL_DUMMY_LOCATION */
421 write_register (RP_REGNUM, pc);
ac954805
SG
422 return sp;
423}
dc1b349d
MS
424#endif /* PUSH_RETURN_ADDRESS */
425
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
431 will be found. */
432
433CORE_ADDR
434v850_frame_saved_pc (fi)
435 struct frame_info *fi;
436{
437 if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
438 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
439 else
440 return v850_find_callers_reg (fi, RP_REGNUM);
441}
442
443void
444get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
445 char *raw_buffer;
446 int *optimized;
447 CORE_ADDR *addrp;
448 struct frame_info *frame;
449 int regnum;
450 enum lval_type *lval;
451{
452 generic_get_saved_register (raw_buffer, optimized, addrp,
453 frame, regnum, lval);
454}
455
456
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:
460 jarl <offset24>, r31
461 trap
462 */
463
464int
465v850_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
466 char *dummy;
467 CORE_ADDR sp;
468 CORE_ADDR fun;
469 int nargs;
470 value_ptr *args;
471 struct type *type;
472 int gcc_p;
473{
474 long offset24;
475 CORE_ADDR call_dummy_start;
476#ifdef NEED_TEXT_START_END
477 extern CORE_ADDR text_end;
478#endif
479
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;
488#endif
489
490 offset24 = (long) fun - (long) call_dummy_start;
491 offset24 &= 0x3fffff;
492 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
493
494 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
495 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
496 return 0;
497}
498
e2810631 499void
dc1b349d 500_initialize_v850_tdep ()
e2810631
SG
501{
502 tm_print_insn = print_insn_v850;
503}
This page took 0.053878 seconds and 4 git commands to generate.