Tue Nov 5 10:21:02 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[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 /* Dummy frame. This saves the processor state just prior to setting up the
32 inferior function call. On most targets, the registers are saved on the
33 target stack, but that really slows down function calls. */
34
35 struct dummy_frame
36 {
37 struct dummy_frame *next;
38
39 char regs[REGISTER_BYTES];
40 };
41
42 static struct dummy_frame *dummy_frame_stack = NULL;
43
44 static CORE_ADDR read_register_dummy PARAMS ((int regno));
45
46 /* Info gleaned from scanning a function's prologue. */
47
48 struct pifsr /* Info about one saved reg */
49 {
50 int framereg; /* Frame reg (SP or FP) */
51 int offset; /* Offset from framereg */
52 int reg; /* Saved register number */
53 };
54
55 struct prologue_info
56 {
57 int framereg;
58 int frameoffset;
59 int start_function;
60 struct pifsr *pifsrs;
61 };
62
63 static CORE_ADDR scan_prologue PARAMS ((CORE_ADDR pc, struct prologue_info *fs));
64 \f
65 /* Scan the prologue of the function that contains PC, and record what we find
66 in PI. PI->fsr must be zeroed by the called. Returns the pc after the
67 prologue. Note that the addresses saved in pi->fsr are actually just frame
68 relative (negative offsets from the frame pointer). This is because we
69 don't know the actual value of the frame pointer yet. In some
70 circumstances, the frame pointer can't be determined till after we have
71 scanned the prologue. */
72
73 static CORE_ADDR
74 scan_prologue (pc, pi)
75 CORE_ADDR pc;
76 struct prologue_info *pi;
77 {
78 CORE_ADDR func_addr, prologue_end, current_pc;
79 struct pifsr *pifsr;
80 int fp_used;
81
82 /* First, figure out the bounds of the prologue so that we can limit the
83 search to something reasonable. */
84
85 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
86 {
87 struct symtab_and_line sal;
88
89 sal = find_pc_line (func_addr, 0);
90
91 if (func_addr == entry_point_address ())
92 pi->start_function = 1;
93 else
94 pi->start_function = 0;
95
96 #if 0
97 if (sal.line == 0)
98 prologue_end = pc;
99 else
100 prologue_end = sal.end;
101 #else
102 prologue_end = pc;
103 #endif
104 }
105 else
106 { /* We're in the boondocks */
107 func_addr = pc - 100;
108 prologue_end = pc;
109 }
110
111 prologue_end = min (prologue_end, pc);
112
113 /* Now, search the prologue looking for instructions that setup fp, save
114 rp, adjust sp and such. We also record the frame offset of any saved
115 registers. */
116
117 pi->frameoffset = 0;
118 pi->framereg = SP_REGNUM;
119 fp_used = 0;
120 pifsr = pi->pifsrs;
121
122 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
123 {
124 int insn;
125
126 insn = read_memory_unsigned_integer (current_pc, 2);
127
128 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
129 || (insn & 0xffe0) == 0x0060 /* jmp */
130 || (insn & 0x0780) == 0x0580) /* branch */
131 break; /* Ran into end of prologue */
132 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
133 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
134 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
135 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
136 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
137 {
138 fp_used = 1;
139 pi->framereg = FP_REGNUM;
140 }
141 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
142 || (fp_used
143 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
144 if (pifsr)
145 {
146 pifsr->framereg = insn & 0x1f;
147 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
148
149 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
150
151 pifsr++;
152 }
153
154 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
155 current_pc += 2;
156 }
157
158 if (pifsr)
159 pifsr->framereg = 0; /* Tie off last entry */
160
161 return current_pc;
162 }
163
164 /* Setup the frame frame pointer, pc, and frame addresses for saved registers.
165 Most of the work is done in scan_prologue().
166
167 Note that when we are called for the last frame (currently active frame),
168 that fi->pc and fi->frame will already be setup. However, fi->frame will
169 be valid only if this routine uses FP. For previous frames, fi-frame will
170 always be correct (since that is derived from v850_frame_chain ()).
171
172 We can be called with the PC in the call dummy under two circumstances.
173 First, during normal backtracing, second, while figuring out the frame
174 pointer just prior to calling the target function (see run_stack_dummy).
175 */
176
177 void
178 v850_init_extra_frame_info (fi)
179 struct frame_info *fi;
180 {
181 struct prologue_info pi;
182 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
183 int reg;
184
185 if (fi->next)
186 fi->pc = FRAME_SAVED_PC (fi->next);
187
188 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
189
190 /* The call dummy doesn't save any registers on the stack, so we can return
191 now. */
192 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
193 {
194 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
195 by assuming it's always FP. */
196 fi->frame = read_register_dummy (SP_REGNUM);
197 return;
198 }
199
200 pi.pifsrs = pifsrs;
201
202 scan_prologue (fi->pc, &pi);
203
204 if (!fi->next && pi.framereg == SP_REGNUM)
205 fi->frame = read_register (pi.framereg) - pi.frameoffset;
206
207 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
208 {
209 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
210
211 if (pifsr->framereg == SP_REGNUM)
212 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
213 }
214 }
215
216 /* Figure out the frame prior to FI. Unfortunately, this involves scanning the
217 prologue of the caller, which will also be done shortly by
218 v850_init_extra_frame_info. For the dummy frame, we just return the stack
219 pointer that was in use at the time the function call was made. */
220
221 CORE_ADDR
222 v850_frame_chain (fi)
223 struct frame_info *fi;
224 {
225 CORE_ADDR callers_pc;
226 struct prologue_info pi;
227
228 /* First, find out who called us */
229
230 callers_pc = FRAME_SAVED_PC (fi);
231
232 if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
233 return read_register_dummy (SP_REGNUM); /* XXX Won't work if multiple dummy frames on stack! */
234
235 pi.pifsrs = NULL;
236
237 scan_prologue (callers_pc, &pi);
238
239 if (pi.start_function)
240 return 0; /* Don't chain beyond the start function */
241
242 if (pi.framereg == FP_REGNUM)
243 return v850_find_callers_reg (fi, pi.framereg);
244
245 return fi->frame - pi.frameoffset;
246 }
247
248 /* Find REGNUM on the stack. Otherwise, it's in an active register. One thing
249 we might want to do here is to check REGNUM against the clobber mask, and
250 somehow flag it as invalid if it isn't saved on the stack somewhere. This
251 would provide a graceful failure mode when trying to get the value of
252 caller-saves registers for an inner frame. */
253
254 CORE_ADDR
255 v850_find_callers_reg (fi, regnum)
256 struct frame_info *fi;
257 int regnum;
258 {
259 /* XXX - Won't work if multiple dummy frames are active */
260 /* When the caller requests RP from the dummy frame, we return PC because
261 that's where the previous routine appears to have done a call from. */
262 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
263 if (regnum == RP_REGNUM)
264 regnum = PC_REGNUM;
265
266 for (; fi; fi = fi->next)
267 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
268 return read_register_dummy (regnum);
269 else if (fi->fsr.regs[regnum] != 0)
270 return read_memory_integer (fi->fsr.regs[regnum], 4);
271
272 return read_register (regnum);
273 }
274
275 CORE_ADDR
276 v850_skip_prologue (pc)
277 CORE_ADDR pc;
278 {
279 CORE_ADDR func_addr, func_end;
280
281 /* See what the symbol table says */
282
283 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
284 {
285 struct symtab_and_line sal;
286
287 sal = find_pc_line (func_addr, 0);
288
289 if (sal.line != 0 && sal.end < func_end)
290 return sal.end;
291 else
292 /* Either there's no line info, or the line after the prologue is after
293 the end of the function. In this case, there probably isn't a
294 prologue. */
295 return pc;
296 }
297
298 /* We can't find the start of this function, so there's nothing we can do. */
299 return pc;
300 }
301
302 /* Save all the registers on the dummy frame stack. Most ports save the
303 registers on the target stack. This results in lots of unnecessary memory
304 references, which are slow when debugging via a serial line. Instead, we
305 save all the registers internally, and never write them to the stack. The
306 registers get restored when the called function returns to the entry point,
307 where a breakpoint is laying in wait. */
308
309 void
310 v850_push_dummy_frame ()
311 {
312 struct dummy_frame *dummy_frame;
313
314 dummy_frame = xmalloc (sizeof (struct dummy_frame));
315
316 read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
317
318 dummy_frame->next = dummy_frame_stack;
319 dummy_frame_stack = dummy_frame;
320 }
321
322 /* Read registers from the topmost dummy frame. */
323
324 CORE_ADDR
325 read_register_dummy (regno)
326 int regno;
327 {
328 return extract_address (&dummy_frame_stack->regs[REGISTER_BYTE (regno)],
329 REGISTER_RAW_SIZE(regno));
330 }
331
332 int
333 v850_pc_in_call_dummy (pc)
334 CORE_ADDR pc;
335 {
336 return dummy_frame_stack
337 && pc >= CALL_DUMMY_ADDRESS ()
338 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
339 }
340
341 /* This routine gets called when either the user uses the `return' command, or
342 the call dummy breakpoint gets hit. */
343
344 struct frame_info *
345 v850_pop_frame (frame)
346 struct frame_info *frame;
347 {
348 int regnum;
349
350 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
351 {
352 struct dummy_frame *dummy_frame;
353
354 dummy_frame = dummy_frame_stack;
355 if (!dummy_frame)
356 error ("Can't pop dummy frame!");
357
358 dummy_frame_stack = dummy_frame->next;
359
360 write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
361
362 free (dummy_frame);
363 }
364 else
365 {
366 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
367
368 for (regnum = 0; regnum < NUM_REGS; regnum++)
369 if (frame->fsr.regs[regnum] != 0)
370 write_register (regnum,
371 read_memory_integer (frame->fsr.regs[regnum], 4));
372
373 write_register (SP_REGNUM, FRAME_FP (frame));
374 }
375
376 flush_cached_frames ();
377
378 return NULL;
379 }
380
381 /* Setup arguments and RP for a call to the target. First four args go in
382 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
383 reference. 64 bit quantities (doubles and long longs) may be split between
384 the regs and the stack. When calling a function that returns a struct, a
385 pointer to the struct is passed in as a secret first argument (always in R6).
386
387 By the time we get here, stack space has been allocated for the args, but
388 not for the struct return pointer. */
389
390 CORE_ADDR
391 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
392 int nargs;
393 value_ptr *args;
394 CORE_ADDR sp;
395 unsigned char struct_return;
396 CORE_ADDR struct_addr;
397 {
398 int argreg;
399 int argnum;
400
401 argreg = ARG0_REGNUM;
402
403 if (struct_return)
404 {
405 write_register (argreg++, struct_addr);
406 sp -= 4;
407 }
408
409 for (argnum = 0; argnum < nargs; argnum++)
410 {
411 int len;
412 char *val;
413 char valbuf[4];
414
415 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
416 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
417 {
418 store_address (valbuf, 4, VALUE_ADDRESS (*args));
419 len = 4;
420 val = valbuf;
421 }
422 else
423 {
424 len = TYPE_LENGTH (VALUE_TYPE (*args));
425 val = (char *)VALUE_CONTENTS (*args);
426 }
427
428 while (len > 0)
429 if (argreg <= ARGLAST_REGNUM)
430 {
431 CORE_ADDR regval;
432
433 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
434 write_register (argreg, regval);
435
436 len -= REGISTER_RAW_SIZE (argreg);
437 val += REGISTER_RAW_SIZE (argreg);
438 argreg++;
439 }
440 else
441 {
442 write_memory (sp + argnum * 4, val, 4);
443
444 len -= 4;
445 val += 4;
446 }
447 args++;
448 }
449
450 write_register (RP_REGNUM, entry_point_address ());
451
452 return sp;
453 }
454 \f
455 void
456 _initialize_sparc_tdep ()
457 {
458 tm_print_insn = print_insn_v850;
459 }
This page took 0.03875 seconds and 4 git commands to generate.