1a5d69a8f6d3806e6296e73335265b734ca36cae
[deliverable/binutils-gdb.git] / gdb / v850-tdep.c
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
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"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Info gleaned from scanning a function's prologue. */
32
33 struct 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
40 struct prologue_info
41 {
42 int framereg;
43 int frameoffset;
44 int start_function;
45 struct pifsr *pifsrs;
46 };
47
48 static CORE_ADDR v850_scan_prologue PARAMS ((CORE_ADDR pc,
49 struct prologue_info *fs));
50 \f
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. */
59
60 static CORE_ADDR
61 v850_scan_prologue (pc, pi)
62 CORE_ADDR pc;
63 struct prologue_info *pi;
64 {
65 CORE_ADDR func_addr, prologue_end, current_pc;
66 struct pifsr *pifsr;
67 int fp_used;
68
69 /* First, figure out the bounds of the prologue so that we can limit the
70 search to something reasonable. */
71
72 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
73 {
74 struct symtab_and_line sal;
75
76 sal = find_pc_line (func_addr, 0);
77
78 if (func_addr == entry_point_address ())
79 pi->start_function = 1;
80 else
81 pi->start_function = 0;
82
83 #if 0
84 if (sal.line == 0)
85 prologue_end = pc;
86 else
87 prologue_end = sal.end;
88 #else
89 prologue_end = pc;
90 #endif
91 }
92 else
93 { /* We're in the boondocks */
94 func_addr = pc - 100;
95 prologue_end = pc;
96 }
97
98 prologue_end = min (prologue_end, pc);
99
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
102 registers. */
103
104 pi->frameoffset = 0;
105 pi->framereg = SP_REGNUM;
106 fp_used = 0;
107 pifsr = pi->pifsrs;
108
109 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
110 {
111 int insn;
112
113 insn = read_memory_unsigned_integer (current_pc, 2);
114
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 */
124 {
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] */
131 if (pifsr)
132 {
133 pifsr->framereg = insn & 0x1f;
134 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
135
136 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
137
138 pifsr++;
139 }
140
141 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
142 current_pc += 2;
143 }
144
145 if (pifsr)
146 pifsr->framereg = 0; /* Tie off last entry */
147
148 return current_pc;
149 }
150
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().
154
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
162 pointer just prior to calling the target function (see run_stack_dummy). */
163
164 void
165 v850_init_extra_frame_info (fi)
166 struct frame_info *fi;
167 {
168 struct prologue_info pi;
169 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
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. */
179 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
180 return;
181
182 pi.pifsrs = pifsrs;
183
184 v850_scan_prologue (fi->pc, &pi);
185
186 if (!fi->next && pi.framereg == SP_REGNUM)
187 fi->frame = read_register (pi.framereg) - pi.frameoffset;
188
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 }
196 }
197
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. */
204
205 CORE_ADDR
206 v850_frame_chain (fi)
207 struct frame_info *fi;
208 {
209 struct prologue_info pi;
210 CORE_ADDR callers_pc, fp;
211
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 */
218
219 /* Caller is NOT a call-dummy, so everything else should just work.
220 Even if THIS frame is a call-dummy! */
221 pi.pifsrs = NULL;
222
223 v850_scan_prologue (callers_pc, &pi);
224
225 if (pi.start_function)
226 return 0; /* Don't chain beyond the start function */
227
228 if (pi.framereg == FP_REGNUM)
229 return v850_find_callers_reg (fi, pi.framereg);
230
231 return fi->frame - pi.frameoffset;
232 }
233
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. */
241
242 CORE_ADDR
243 v850_find_callers_reg (fi, regnum)
244 struct frame_info *fi;
245 int regnum;
246 {
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));
253
254 return read_register (regnum);
255 }
256
257 /* Function: skip_prologue
258 Return the address of the first code past the prologue of the function. */
259
260 CORE_ADDR
261 v850_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
274 if (sal.line != 0 && sal.end < func_end)
275 return sal.end;
276 else
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. */
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
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. */
290
291 void
292 v850_pop_frame (frame)
293 struct frame_info *frame;
294 {
295 int regnum;
296
297 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
298 generic_pop_dummy_frame ();
299 else
300 {
301 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
302
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)));
308
309 write_register (SP_REGNUM, FRAME_FP (frame));
310 }
311
312 flush_cached_frames ();
313 }
314
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).
322
323 Stack space for the args has NOT been allocated: that job is up to us.
324 */
325
326 CORE_ADDR
327 v850_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;
336 int len = 0;
337 int stack_offset;
338
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! ) */
348 argreg = ARG0_REGNUM;
349
350 /* the struct_return pointer occupies the first parameter-passing reg */
351 if (struct_return)
352 write_register (argreg++, struct_addr);
353
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. */
362 for (argnum = 0; argnum < nargs; argnum++)
363 {
364 int len;
365 char *val;
366 char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
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)
382 if (argreg <= ARGLAST_REGNUM)
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 {
395 write_memory (sp + stack_offset, val, 4);
396
397 len -= 4;
398 val += 4;
399 stack_offset += 4;
400 }
401 args++;
402 }
403 return sp;
404 }
405
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
411 CORE_ADDR
412 v850_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);
422 return sp;
423 }
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
433 CORE_ADDR
434 v850_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
443 void
444 get_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
464 int
465 v850_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
499 void
500 _initialize_v850_tdep ()
501 {
502 tm_print_insn = print_insn_v850;
503 }
This page took 0.051179 seconds and 4 git commands to generate.