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